Qt 悬停在按钮上并触发功能? [英] Qt Hovering over a button and triggering a function?

查看:59
本文介绍了Qt 悬停在按钮上并触发功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果鼠标光标悬停在 QButton 上,触发功能的正确方法是什么?

What is the proper way of triggering a function if the mouse cursor hovers over a QButton?

更准确地说,我有一个名为 statusLabel 的标签,每当我将鼠标光标悬停在按钮上时,它都会显示消息,并且只要鼠标不在按钮上,它就会恢复为空字符串.

To be more precise I have a label called statusLabel which should show message whenever I hover my mouse cursor over a button and should revert back to empty string whenever the mouse is not over it.

推荐答案

您必须创建自己的类,派生自 QPushButton(或您想在其中捕获悬停事件的任何类).

You have to create your own class, derived of QPushButton (or whichever class you want to capture hover events in).

在这个类中,你可以覆盖QWidget::enterEvent() 检测鼠标何时悬停在您的小部件上并调用您的函数.例如,您可以做的是发出一个自定义信号,触发擦除您的标签.

In this class, you can override QWidget::enterEvent() to detect when the mouse hovers your widget and call your function. What you can for example do is emit a custom signal that will trigger the erasing of your label.

这篇关于Qt 悬停在按钮上并触发功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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