python - 如何使用 QAbstractTableModel 模型控制其他小部件?

标签 python qt model pyqt qtableview

QTableViewQAbstractTableModel 控制。它是填充其内容的模型。当用户双击QTableView的项目之一然后输入一个新的结果时,该模型会通过写入来修改self.items变量。值。

对话框底部的按钮最初被禁用。我希望用户输入新值后立即启用此按钮(通过双击 QTableView 的任何一项并输入字符串或数字)。

本质上我希望模型控制按钮的状态。我怎样才能实现它?

enter image description here

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os

class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items =[ 
                    ['Row0_Column0','Row0_Column1','Row0_Column2'],
                    ['Row1_Column0','Row1_Column1','Row1_Column2'],
                    ['Row2_Column0','Row2_Column1','Row2_Column2']
                    ]

    def flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

    def rowCount(self, parent):
        return len(self.items)       
    def columnCount(self, parent):
        return 3

    def data(self, index, role):
        if not index.isValid(): return QVariant()
        row=index.row()
        column=index.column()

        if row>len(self.items): return QVariant()        
        if column>len(self.items[row]): return QVariant()

        if role == Qt.EditRole:
            return QVariant(self.items[row][column])

        if role == Qt.DisplayRole:            
            return QVariant(self.items[row][column])

        return QVariant()

    def setData(self, index, value, role=Qt.EditRole):
        if index.isValid():            
            if role == Qt.EditRole:
                row = index.row()
                column=index.column()
                if row>len(self.items) or column>len(self.items[row]):
                    return False
                else:
                    self.items[row][column]=value  
                    return True
        return False

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        tablemodel=Model(self)               
        tableview=QTableView(self) 
        tableview.setModel(tablemodel)    
        layout=QVBoxLayout(self)
        layout.addWidget(tableview)
        self.button=QPushButton('Push Me')
        self.button.setDisabled(True)
        layout.addWidget(self.button)
        self.setLayout(layout)      

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

最佳答案

Martin 的评论已经是一个很好的起点,但在您的自定义模型中,您还必须在 setData 方法中发出 dataChanged 信号才能使其工作。

然后您还可以定义自己的自定义信号并发出它们并连接它们。但是,这是带有 QAbstractItemModel 的 dataChanged 信号的示例:

from PySide import QtGui, QtCore

class Model(QtCore.QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QtCore.QAbstractTableModel.__init__(self, parent, *args)
        self.items =[ 
                    ['Row0_Column0','Row0_Column1','Row0_Column2'],
                    ['Row1_Column0','Row1_Column1','Row1_Column2'],
                    ['Row2_Column0','Row2_Column1','Row2_Column2']
                    ]

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable

    def rowCount(self, parent):
        return len(self.items)

    def columnCount(self, parent):
        return 3

    def data(self, index, role):
        if not index.isValid():
            return None

        row=index.row()
        column=index.column()

        if row > len(self.items):
            return None
        if column > len(self.items[row]):
            return None

        if role == QtCore.Qt.EditRole:
            return self.items[row][column]

        if role == QtCore.Qt.DisplayRole:
            return self.items[row][column]

        return None

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        if index.isValid():            
            if role == QtCore.Qt.EditRole:

                row = index.row()
                column = index.column()

                if row > len(self.items) or column > len(self.items[row]):
                    return False
                else:
                    self.items[row][column] = value
                    self.dataChanged.emit(index, index) # emit the signal
                    return True
        return False

class MyWindow(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)

        tablemodel = Model(self)
        tablemodel.dataChanged.connect(self.data_changed) # connect dataChanged signal
        tableview = QtGui.QTableView(self)
        tableview.setModel(tablemodel)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(tableview)

        self.button = QtGui.QPushButton('Push Me')
        self.button.setDisabled(True) # initially disabled

        layout.addWidget(self.button)
        self.setLayout(layout)

    def data_changed(self, topleft_index, bottom_right_index):
        # just enable the button
        self.button.setEnabled(True)

app = QtGui.QApplication([])
w = MyWindow()
w.show()
app.exec_()

注释行是重要的。

关于python - 如何使用 QAbstractTableModel 模型控制其他小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092837/

相关文章:

Python登录邮件服务器认证错误

python - 在PyQt中使用QAudioOutput播放声音

android - 如何将 "Chrome"(或等效项)作为线程而不是进程启动?

python - 无需从目录中提取即可恢复 Tensorflow 模型

python - Django 使用字符串来过滤模型以避免重复

asp.net-mvc - EF 映射对象的不兼容数据读取器异常

python - 混合 Bash-Python 代码段中变量的双引号与单引号

android - 使用Python或adb命令读取Android UI文本?

python - 如何修复 Django 中的 'The QuerySet value for an exact lookup must be limited to one result using slicing.' 错误

c++ - 如何在标题中设置 QTextStream?