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

2024-09-25 14:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

单击tableView's item将打开一个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_()

Tags: selfappindexmodelreturninitdefitems
1条回答
网友
1楼 · 发布于 2024-09-25 14:25:00

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调用它:

^{pr2}$

当然,理想情况下,您应该在init中定义编辑器计数字典,并且不需要在editorCount()方法本身中进行太多的异常处理。在

相关问题 更多 >