amancalledScott
amancalledScott

Reputation: 43

How to signal cell entered & cell left in QTableView

In switching from QTableWidget to QTableView to improve the speed of my GUI, I've come to realize that there is no equivalent cellEntered signal available for QTableView. How can I achieve that?

In this GUI, I have a popup window with a QTableView that shows coordinates of markers placed on an image in a separate window. I need to highlight the markers in the image window as the cursor is moved over the corresponding rows or cells in the QTableView coordinates table. So I need to be able to emit a signal, not just highlight the row in the coordinate table.

Upvotes: 1

Views: 1464

Answers (2)

MiaPlan.de
MiaPlan.de

Reputation: 122

Since the second part of the question has been left unanswered, I want to address the "How to catch the cell or item leave" problem.

Because there is no cellLeft or ItemLeft event, the cellEntered event of the surrounding cells must be used. To know when a cell has been left, we store each entered row and column and decide when a leave event occurred.

class LeaveEvent(QtWidgets.QMainWindow):

    def __init__(self):
        self.table.cellEntered.connect(self.on_table_cell_entered)
        self.prev_idxs = -1, -1

    def on_table_cell_entered(self, row, column):
        if (row, column) != self.prev_idxs:
            print(f'previous cell left: {self.prev_idxs}')

        self.prev_idxs = row, column

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 243907

An equivalent to cellEntered signal is the entered signal:

from PyQt5 import QtGui, QtWidgets


def main():
    app = QtWidgets.QApplication([])

    model = QtGui.QStandardItemModel(5, 5)

    view = QtWidgets.QTableView()
    view.setModel(model)
    view.setMouseTracking(True)

    def on_entered(index):
        print(index.row(), index.column())

    view.entered.connect(on_entered)

    view.show()

    app.exec_()


if __name__ == "__main__":
    main()

Upvotes: 2

Related Questions