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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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 necessarycellExited
/itemExited
signals, and then shows how everything can be hooked up to change the item background when hovering with the mouse:You can achieve your goal pretty easily using the proper signals as proved by the following simple code:
You may be interested in other signals too, especially
itemEntered
. However, if you want total control over the editing and display of items then using delegates (via theQTableWidget.setItemDelegate
method) is strongly recommended.UPDATE:
sorry, I had forgotten the second part of the problem i.e. what happens when the mouse exits a cell. Even then the problem can be solved easily without using events. See the updated code, please.
There are no events based on QTableWidgetItem, but you can do this:
mouseMoveEvent()
of QTableWidget, you can get the mouse position;itemAt()
method to get the item under your mouse cursor;This may simalute what you want.