Search code examples
pythonpyqtsignals-slotsqtableview

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.


Solution

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