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.
For the tableView item to get selected the mousePressEvent
should be able to go all the way through the delegate item to to tableView. With the exception of the row 0 all other rows of indexes are not selected. How to make it work for all model indexes?
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_()
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)
table = self.parent().parent() # added by me here
tableView.selectRow(0) # to fix the issue with tableView row not getting selected on delegated click.
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()
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_()
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()