1

Clicking the tableView's item opens a PersistentEditor: it defaults to QSpinBox for the first column (since integer data) and QLineEdit for two others.

onClick I would like to query how many persistent editors have been already opened for the clicked row.

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_()
4

1 回答 1

1

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

于 2016-05-07T23:30:43.633 回答