【问题标题】:How to query open persistent delegate item如何查询打开的持久委托项目
【发布时间】:2016-05-07 22:46:24
【问题描述】:

单击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_()

【问题讨论】:

    标签: python qt pyqt qtableview qabstracttablemodel


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多