4

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?

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

Solution posted by user1034749:

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_()
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

1 Answers1

3

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()
fghj
  • 8,898
  • 4
  • 28
  • 56
  • All I want is to make sure the tableView's cell gets selected when you click the delegated by QLineEdit cell (left column). Please show how to achieve it without doing the strange things. – alphanumeric May 07 '16 at 17:28
  • I posted your solution under the "Suggestions made by others" section in my original question. Your approach works. But it select the tableView row wrong. – alphanumeric May 07 '16 at 17:30
  • Clicking the delegated item never misses the delegated`s mousePressEvent` method. That is great! But once in a while clicking the delegated item cell does not select a tableView row (to replicate it: deselect all the tableView's rows by clicking an empty area in tableView. Then click the same delegated item that was clicked last time. The `onClick` method prints out that it gets an event. But the row remains deselected.). How to make sure the table row is selected every time the delegated item gets clicked? – alphanumeric May 07 '16 at 18:06
  • putting a line `tableView.selectRow(0)` in `mouseReleaseEvent` fixes the selection issue. – alphanumeric May 07 '16 at 18:36
  • .selectRow(0) solution won't work with Ctrl or Shift key modifies – alphanumeric May 07 '16 at 19:03
  • @spootnx You can reimplement `QApplication.notify` to catch all events, send to any objects, and find out who `eat` your event `once in a while` – fghj May 07 '16 at 20:03