pythonpyqtqtablewidgetmousehoverqtablewidgetitem

How to catch mouse over event of QTableWidget item in pyqt?


what I want to do is to change the color of a QTableWidget item, when I hover with the mouse over the item of my QTableWidget.


Solution

  • Firstly, the table widget needs to have mouse-tracking switched on to get the hover events.

    Secondly, we need to find some signals that tell us when the mouse enters and leaves the table cells, so that the background colours can be changed at the right times.

    The QTableWidget class has the cellEntered / itemEntered signals, but there is nothing for when the mouse leaves a cell. So, we will need to create some custom signals to do that.

    The TableWidget class in the demo script below sets up the necessary cellExited / itemExited signals, and then shows how everything can be hooked up to change the item background when hovering with the mouse:

    from PyQt4 import QtGui, QtCore
    
    class TableWidget(QtGui.QTableWidget):
        cellExited = QtCore.pyqtSignal(int, int)
        itemExited = QtCore.pyqtSignal(QtGui.QTableWidgetItem)
    
        def __init__(self, rows, columns, parent=None):
            QtGui.QTableWidget.__init__(self, rows, columns, parent)
            self._last_index = QtCore.QPersistentModelIndex()
            self.viewport().installEventFilter(self)
    
        def eventFilter(self, widget, event):
            if widget is self.viewport():
                index = self._last_index
                if event.type() == QtCore.QEvent.MouseMove:
                    index = self.indexAt(event.pos())
                elif event.type() == QtCore.QEvent.Leave:
                    index = QtCore.QModelIndex()
                if index != self._last_index:
                    row = self._last_index.row()
                    column = self._last_index.column()
                    item = self.item(row, column)
                    if item is not None:
                        self.itemExited.emit(item)
                    self.cellExited.emit(row, column)
                    self._last_index = QtCore.QPersistentModelIndex(index)
            return QtGui.QTableWidget.eventFilter(self, widget, event)
    
    class Window(QtGui.QWidget):
        def __init__(self, rows, columns):
            QtGui.QWidget.__init__(self)
            self.table = TableWidget(rows, columns, self)
            for column in range(columns):
                for row in range(rows):
                    item = QtGui.QTableWidgetItem('Text%d' % row)
                    self.table.setItem(row, column, item)
            layout = QtGui.QVBoxLayout(self)
            layout.addWidget(self.table)
            self.table.setMouseTracking(True)
            self.table.itemEntered.connect(self.handleItemEntered)
            self.table.itemExited.connect(self.handleItemExited)
    
        def handleItemEntered(self, item):
            item.setBackground(QtGui.QColor('moccasin'))
    
        def handleItemExited(self, item):
            item.setBackground(QtGui.QTableWidgetItem().background())
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window(6, 3)
        window.setGeometry(500, 300, 350, 250)
        window.show()
        sys.exit(app.exec_())