如何在Qt中实现QHoverEvent? [英] How do I implement QHoverEvent in Qt?

查看:1353
本文介绍了如何在Qt中实现QHoverEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚用C ++学习Qt。我已经成功地实现了信号和插槽来捕获标准事件,如 ButtonPushed()等。但是,当我将鼠标悬停在鼠标上时,我想要有一个函数, code> QLabel 。看起来 QHoverEvent 会做我需要的,但我似乎不能找到有关如何实现这一点的任何教程或示例。它的功能与信号和插槽相同吗?我试过:

  connect(ui.lbl_test,SIGNAL(QHoverEvent),this,SLOT(TestFunc(QEvent :: Type type, const QPoint& pos,const QPoint& oldPos))); 

..但是当我悬停在标签上时,该函数没有被调用。



这是功能,头文件中列出的公共插槽:

  void MyDialog :: TestFunc(QEvent :: Type type,const QPoint& pos,const QPoint& oldPos){
QMessageBox :: information(this,tr(Hey),tr(Listen! );
}

任何人都可以帮我弄清楚或指出一个很好的例子? / p>

编辑:



在阅读以下文章后,我发现没有 setFlag() / code>成员调用我的标签小部件,但我确实尝试:

  ui.lbl_test-> setMouseTracking (真正); 
connect(ui.lbl_test,SIGNAL(ui.lbl_test-> mouseMoveEvent()),this,SLOT(TestFunc(QMouseEvent * event)));

并更新 TestFunc()。但是,当鼠标悬停时,仍然没有任何反应。



看完后我不确定 QLabel 甚至继承了mouseMoveEvent()甚至从 QWidget 。如果这是真的,是否有一个小部件,或一个继承它的对象的列表?我可以从他们网站上的文档中得知,对象有多少继承的功能。

解决方案

使用信号和插槽这个目的不会起作用。



mouseMoveEvent()不是信号或元方法,不能连接到一个插槽。



将小部件类重新分类并覆盖 mouseMoveEvent()将允许您获取鼠标 - 移动事件,但这是一个非常重量级的方式来完成这个(并增加一个类到你的源代码基础)。



相反,考虑实现一个<$ c $你的 MyDialog 类中的c> eventFilter()方法,并将其安装在 QLabel 上。使用此事件过滤方法,您可以截取给定的 QObject 实例的所有事件。



这是文档在事件过滤器上。



http://doc.qt.io/qt-4.8/eventsandfilters.html#event-filters



另外,通过查看代码示例,我建议你花一点时间调查微软的 SIGNAL() SLOT()。您可以在 $ QTDIR / src / corelib / kernel / qobjectdefs.h


中查看它们的定义

I'm just learning Qt with C++. I have successfully implemented signals and slots to trap standard events like ButtonPushed(), etc. However, I want to have a function called when I mouse over and mouse out of a QLabel. It looks like QHoverEvent will do what I need, but I can't seem to find any tutorials or examples on how to implement this. Is it done the same way as signals and slots?. I tried:

connect(ui.lbl_test, SIGNAL(QHoverEvent), this, SLOT(TestFunc(QEvent::Type type, const QPoint & pos, const QPoint & oldPos)));

.. but the function didn't get called when I hovered over the label.

Here is the function, listed in the header file as a public slot:

void MyDialog::TestFunc(QEvent::Type type, const QPoint & pos, const QPoint & oldPos) {
     QMessageBox::information(this, tr("Hey"), tr("Listen!"));
}

Can anyone help me figure this out or point me to a good example?

EDIT:

After reading a post below, I found no setFlag() member to call for my label widget, but I did try:

    ui.lbl_test->setMouseTracking(true);
    connect(ui.lbl_test, SIGNAL(ui.lbl_test->mouseMoveEvent()), this, SLOT(TestFunc(QMouseEvent *event)));

And updated TestFunc() accordingly. But still nothing happens when I mouse over.

After looking I am not sure QLabel even inherits the mouseMoveEvent() even from QWidget. If this is true, is there a widget that does, or a list of objects that inherit it somewhere?. All I can tell from the documentation on their site is how many inherited functions an object has..

解决方案

Using signals and slots for this purpose isn't going to work.

mouseMoveEvent() is not a signal or meta-method and cannot be connected to a slot.

Subclassing the widget class and overriding mouseMoveEvent() will allow you to get mouse-move-events, but that is a very heavyweight way to accomplish this (and adds one more class to your source base).

Instead, consider implementing an eventFilter() method on your MyDialog class and installing it on the QLabel. With this event filter method, you can intercept all the events for a given QObject instance.

Here is the documentation on Event Filters.

http://doc.qt.io/qt-4.8/eventsandfilters.html#event-filters

Additionally, through looking at the code sample, I'd recommend you take a moment to investigate what the SIGNAL() and SLOT() macros do. You can see how they are defined in $QTDIR/src/corelib/kernel/qobjectdefs.h

这篇关于如何在Qt中实现QHoverEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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