PyQt:如何捕获QTableWidget标题的鼠标悬停事件? [英] PyQt: How to catch mouse-over-event of QTableWidget-headers?

查看:589
本文介绍了PyQt:如何捕获QTableWidget标题的鼠标悬停事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是每次我将鼠标悬停在QTableWidget的horizo​​ntalHeaders上方时,更改QLable的文本.我怎样才能做到这一点?每次经过新的标头时,我都需要一个信号和标头的索引.希望你们中的某人有一个主意.必须有一个功能,因为如果将鼠标悬停在标题上,标题的背景就会改变.

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.

推荐答案

使用安装了self.filter时,您会收到必要事件的通知,并可以做出相应的响应.

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

更新:看来HoverEvent并不是我们所需要的.为了获取悬停事件,您需要使用 .从有关此属性的文档中:

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:

当鼠标进入或离开窗口小部件时,强制Qt生成绘制事件.在实现自定义样式时通常使用此功能.有关详细信息,请参见样式示例.

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)

正如您在上面看到的那样,主要概念仍然适用.我们要么需要一个事件过滤器,以便我们可以监视事件,要么我们需要对QHeaderView进行子类化,以便为我们提供必要的信息.

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.

这篇关于PyQt:如何捕获QTableWidget标题的鼠标悬停事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆