spootnx spootnx - 7 months ago 135
Python Question

How to pass mousePressEvent from QItemDelegate to QTableView

The code creates a single QTableView. Left column is pre-populated with QLineEdits delegates. Right column is not populated with any delegates.

When the left-column's delegated QLineEdit is clicked the 'clicked' signal is blocked by the delegated item and tableView "cell" never gets selected.

To make sure the tableView item is selected I want to make sure the

mousePressEvent
is sent all the way to tableView. But by some reason only the index at 0,0 gets selected. How to make it work for all tableView indexes?

enter image description here

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

class LineEdit(QTextEdit):
def __init__(self, parent=None):
super(LineEdit, self).__init__(parent)
def mousePressEvent(self, event):
tableView = self.parent().parent()
tableView.mousePressEvent(event)

class Delegate(QItemDelegate):
def createEditor(self, parent, option, index):
return LineEdit(parent)

def onClick(index):
print 'tableView.onClick:', index

tableView = QTableView()
tableView.setModel(QStandardItemModel(4, 2))
tableView.clicked.connect(onClick)
tableView.setItemDelegate(Delegate())

for row in range(4):
tableView.openPersistentEditor(tableView.model().index(row, 0))

tableView.show()
app.exec_()


Suggestions made by others:



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

class LineEdit(QTextEdit):
def __init__(self, parent=None):
super(LineEdit, self).__init__(parent)

def mouseReleaseEvent(self, event):
super(LineEdit, self).mouseReleaseEvent(event)
event.ignore()

def mousePressEvent(self, event):
super(LineEdit, self).mousePressEvent(event)
event.ignore()

class Delegate(QItemDelegate):
def createEditor(self, parent, option, index):
return LineEdit(parent)

def onClick(index):
print 'tableView.onClick:', index
selectedIndexes = tableView.selectionModel().selectedRows()
print selectedIndexes

tableView = QTableView()
tableView.setSelectionBehavior(QTableView.SelectRows)
tableView.setModel(QStandardItemModel(4, 2))
tableView.clicked.connect(onClick)
tableView.setItemDelegate(Delegate())

for row in range(4):
tableView.openPersistentEditor(tableView.model().index(row, 0))

tableView.show()
app.exec_()

Answer

to pass through event, you need just ignore it, like this:

 def mouseReleaseEvent(self, event):
        print "mouse release"
        super(LineEdit, self).mouseReleaseEvent(event)
        event.ignore()

 def mousePressEvent(self, event):
        print "mouse press"
        super(LineEdit, self).mousePressEvent(event)
        event.ignore()