Qt:MouseMove不起作用 [英] Qt: MouseMove not functioning

查看:610
本文介绍了Qt:MouseMove不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Qt应用程序中,我需要跟踪鼠标的移动.为此,我创建了一个eventfilter,并以此正确安装了它:

In my Qt application, I need to track mouse movement. For that, I created an eventfilter and I installed it correctly as this:

bool iArmMainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseMove)//not working
    {
        iarm->printStatus("hi"); //this is for debugging

    }
    if (event->type() == QEvent::MouseButtonPress){
                //Here some staff working correctly
        }
//other staff
}

问题在于事件类型MouseMove不起作用.

The problem is that the event type MouseMove does not work.

有什么主意吗?

推荐答案

您应该对Qt说,您想通过

You should say to Qt, that you want to get mouse move events via setMouseTracking() function. Take an attention, that you should call it before installing a filter (say in c-tor of your widget's subclass). I'll recommend you a little bit easier way instead of installing event filter: just overwrite QWidget::mouseMoveEvent() in your widget's subclass. Like this:

// header:
class MyWidget {
    ...
    void mouseMoveEvent( QMouseEvent * event );
};

// source:
MyWidget::MyWidget() {
    setMouseTracking(true);  //enables mouse tracking
}

void MyWidget::mouseMoveEvent( QMouseEvent * event ) {
    // do what you want
}

这篇关于Qt:MouseMove不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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