python - 如何查询打开的持久委托(delegate)项

标签 python qt pyqt qtableview qabstracttablemodel

单击tableView的项目将打开一个PersistentEditor:第一列默认为QSpinBox(由于整数数据)和QLineEdit 另外两个。

onClick 我想查询已为单击的行打开了多少个持久编辑器。

enter image description here

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Model(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']]

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

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 3 
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 3

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

        if role in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]:
            return self.items[index.row()][index.column()]

def onClick(index):
    tableView.openPersistentEditor(tableView.model().index(index.row(), index.column()))
    print 'clicked index:  %s'%index

tableModel=Model()
tableView=QtGui.QTableView() 
tableView.setModel(tableModel)
tableView.clicked.connect(onClick)

tableView.show()
app.exec_()

最佳答案

QT 可能会提供一种方法来实现您想要的功能。如果是这样,我假设您已经浏览了文档但没有找到任何内容。

我想知道在你的模型上定义一个 editorCount() 方法是否有效,如下所示:

def editorCount(index):
    try:
        rval = self.editor_count[index]
        self.editor_count[index] += 1
    except AttributeError:
        self.editor_count = {}
        self.editor_count[index] = 1
        rval = 0
    except KeyError:
        self.editor_count[index] = 1
        rval = 0
    return rval

然后让 onClick 调用它:

def onClick(index):
    tableView.openPersistentEditor(tableView.model().index(index.row(), index.column()))
    current_editors = tableView.model().editor_count()
    print 'clicked index:  %s'%index

当然,理想情况下,您可以在 init 中定义 editor_count 字典,并且不需要在 editorCount() 方法本身中进行如此多的异常处理。

关于python - 如何查询打开的持久委托(delegate)项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37094472/

相关文章:

python - 其他线程中的 QMessageBox

qt - QGraphicsView/QGraphicsScene 大小匹配

python - PyQt5虚拟键盘(TypeError : missing 1 required positional argument)

python - 获取 url 时出现 UnicodeEncodeError

python - 没有循环 Python 的数字根

python - 使用循环过滤带有关键字列表的字符串列表

qt - 如何在Qt Qdebuger中更改cv::mat的数据类型

Qt QLineEdit 输入验证

python - Django模板ifequal小数比较

qt - 如何从 QPixMap 中删除内容