views:

34

answers:

1

Hi guys,

what I want to do is to change the text of a QLable, everytime I hover with the mouse over the horizontalHeaders of my QTableWidget. How can I do that? Everytime I'm over a new header I need a signal and the index of the header. Hope someone of you has an idea. There must be a function, because if you hover over the headers, the background of the header changes.

+1  A: 

Install an event filter that on the horizontalHeader() by using QObject.installEventFilter():

class HeaderViewFilter(QObject):
    # ...
    def eventFilter(self, object, event):
        if event.type() == QEvent.HoverEvent:
            pass # do something useful
            # you could emit a signal here if you wanted

self. filter = HeaderViewFilter()
horizontalHeader().installEventFilter(self.filter)

With self.filter in place, you'll be notified of the necessary events and can respond accordingly.

UPDATE: it looks like HoverEvent isn't quite what we need. In order to get hover events, you need to setAttribute needs to be called with Qt::WA_Hover. From the documentation on this attribute:

Forces Qt to generate paint events when the mouse enters or leaves the widget. This feature is typically used when implementing custom styles; see the Styles example for details.

So yes, it generates events only when you enter or leave the widget.

Since the same header is used for all rows or all columns, we'll actually want to know about where the mouse is within the widget. Here's some new code which should handle mouse moves:

class HeaderViewFilter(QObject):
    def __init__(self, parent, header, *args):
        super(HeaderViewFilter, self).__init__(parent, *args)
        self.header = header
    def eventFilter(self, object, event):
        if event.type() == QEvent.MouseMove:
            logicalIndex = self.header.logicalIndexAt(event.pos())
            # you could emit a signal here if you wanted

self.filter = HeaderViewFilter()
self.horizontalHeader = yourView.horizontalHeader()
self.horizontalHeader.setMouseTracking(1)
self.horizontalHeader.installEventFilter(self.filter)

As you can see above, the main concept still applies. We either need an event filter so that we can watch for events or we need to subclass QHeaderView so that it will provide us with the necessary information.

Kaleb Pederson
Thank you again K.P.. I'll try that.
I tried it, but I have still a problem. If the cursor hovers over the horizontalHeader-bar, I get only a QHeaderView. And if I'm hovering over the headers, I don't get any HoverEnter event. Only the first time when I'm entering the header-bar. Can you please give me another hint.
I just added some code demonstrating how to use the mouse move event.
Kaleb Pederson
Awesome! logicalIndexAt(event.pos()) is what I was looking for. Now it works. Thank you very much.
@xinor - By the way, whether or not you accept an answer, you should upvote (by using the up arrow) any answers you think are useful. Thanks.
Kaleb Pederson