本文介绍了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说,您想通过 setMouseTracking()函数.请注意,在安装过滤器之前(在您的小部件的子类的c-tor中)应该在 之前将其称为.我建议您使用一种简单的方法,而不是安装事件过滤器:只需覆盖小部件的子类中的QWidget :: mouseMoveEvent().像这样:

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不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:14