【问题标题】:How to stop Item Delegate from blocking mousePressEvent如何阻止 Item Delegate 阻止 mousePressEvent
【发布时间】:2023-03-11 19:33:01
【问题描述】:

QTableView 被指定为 QAbstractTableModel 作为模型。并且ItemDelegate(QItemDelegate) 被分配了tableView.openPersistentEditor。现在,当点击 tableView 时,事件不会一直传播到 tableView(它是否被委托的QLineEditor 阻止)。

有什么方法可以传递 QLineEdit'smousePressEventevent that is passed through theItemDelegatetoQTableView 的 modelwhen the tableView's item is clicked even while it is occupied byQItemInstance? Is there anyQItemDelegate` 标志来完成任务?

代码创建了一个带有模型和委托的 tableView。 通过单击第 0 列或第 1 列触发的事件不会传播到 tableView 的项目,因为该项目保持取消选中状态。

取消注释带有tableView.setSelectionBehavior(QtGui.QTableView.SelectRows) 的行可以解决问题。是吗?

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

class LineEdit(QtGui.QLineEdit):
    def __init__(self, parent=None, total=20):
        super(LineEdit, self).__init__(parent=parent)

    def mousePressEvent(self, event):
        print 'mousePressEvent', event
        super(LineEdit, self).mousePressEvent(event)

class Delegate(QtGui.QItemDelegate):
    def __init__(self):
        QtGui.QItemDelegate.__init__(self)

    def createEditor(self, parent, option, index):
        if index.column()==0:
            lineedit=LineEdit(parent)
            return lineedit

        elif index.column()==1:
            combo=QtGui.QComboBox(parent)
            return combo

    def setEditorData(self, editor, index):
        row = index.row()
        column = index.column()
        value = index.model().items[row][column]
        if isinstance(editor, QtGui.QComboBox):
            editor.addItems(['Somewhere','Over','The Rainbow'])
            editor.setCurrentIndex(index.row())
        if isinstance(editor, QtGui.QLineEdit):
            editor.setText('Somewhere over the rainbow')

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.ItemIsSelectable | 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 
        row = index.row()
        column = index.column()
        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            return self.items[row][column]

def tableViewClicked(index):
    print 'clicked:  %s'%index


tableModel=Model()
tableView=QtGui.QTableView() 
tableView.setModel(tableModel)
tableView.setItemDelegate(Delegate())
# tableView.setSelectionBehavior(QtGui.QTableView.SelectRows)
tableView.clicked.connect(tableViewClicked)

for row in range(tableModel.rowCount()):
    for column in range(tableModel.columnCount()):
        index=tableModel.index(row, column)
        tableView.openPersistentEditor(index)

tableView.show()
app.exec_()

【问题讨论】:

    标签: python qt pyqt qtableview qitemdelegate


    【解决方案1】:

    为什么不在委托项的函数中使用模型对象指针?例如,将您的委托项目的信号连接到模型的插槽,或者在您的委托项目的事件处理程序中调用模型的函数?

    【讨论】:

    • 在委托项的事件处理程序中调用模型的函数会起作用。有些人不喜欢这样的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2019-12-04
    • 2018-12-02
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2011-10-13
    • 2016-06-14
    相关资源
    最近更新 更多