简体   繁体   English

PyQt:如何捕获QTableWidget标题的鼠标悬停事件?

[英]PyQt: How to catch mouse-over-event of QTableWidget-headers?

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. 每次我将鼠标悬停在QTableWidget的horizo​​ntalHeaders上时,我想要做的就是更改QLable的文本。 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. 必须有一个函数,因为如果将鼠标悬停在标题上,标题的背景就会改变。

Install an event filter that on the horizontalHeader() by using QObject.installEventFilter() : 使用QObject.installEventFilter()将事件过滤器安装在horizo​​ntalHeader ()上

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. 设置好self.filter后,您会收到必要事件的通知,并可以做出相应的响应。

UPDATE : it looks like HoverEvent isn't quite what we need. 更新 :看起来HoverEvent不是我们所需要的。 In order to get hover events, you need to setAttribute needs to be called with Qt::WA_Hover . 为了获取悬停事件,需要使用Qt::WA_Hover调用setAttribute。 From the documentation on this attribute: 从有关此属性的文档中:

Forces Qt to generate paint events when the mouse enters or leaves the widget. 当鼠标进入或离开小部件时,强制Qt生成绘制事件。 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. 我们要么需要一个事件过滤器,以便我们可以监视事件,要么需要对QHeaderView进行子类QHeaderView以便它将为我们提供必要的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM