alphanumeric
alphanumeric

Reputation: 19329

How to stop Item Delegate from blocking mousePressEvent

The QTableView was assigned QAbstractTableModel as a model. And ItemDelegate(QItemDelegate) was assigned with tableView.openPersistentEditor. Now when tableView is clicked the event does not propagate all the way to the tableView (does it get blocked by a delegated QLineEditor).

What would be a way to pass the QLineEdit'smousePressEventevent that is passed through theItemDelegatetoQTableView's modelwhen the tableView's item is clicked even while it is occupied byQItemInstance? Is there anyQItemDelegate` flags that can be used to get things done?

The code creates a single tableView with model and delegate. The event triggered by clicking the column 0 or column 1 does not propagate to the tableView's item since the item stays deselected.

Uncommenting a line with tableView.setSelectionBehavior(QtGui.QTableView.SelectRows) fixes the problem. Does it?

enter image description here

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

Upvotes: 1

Views: 398

Answers (1)

qd.ma
qd.ma

Reputation: 26

why not use a model object pointer in your delegate item's function ? e.g connect your delegate item's signal to model's slots, or call model's function in your delegate item's event handler?

Upvotes: 1

Related Questions