Qt: MouseMove not functioning

早过忘川 提交于 2019-12-11 13:14:21

问题


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
}

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

Any idea?


回答1:


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
}


来源:https://stackoverflow.com/questions/16397217/qt-mousemove-not-functioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!