如何查询打开的持久委托项

How to query open persistent delegate item

单击 tableView's item 打开 PersistentEditor:第一列默认为 QSpinBox(因为是整数数据),其他两列默认为 QLineEdit

onClick 我想查询一下,点击行已经打开了多少持久化编辑器。

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() 方法本身中进行如此多的异常处理。