1

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?

Iuliu
  • 4,001
  • 19
  • 31
  • Not working in what way? – Joseph Mansfield May 06 '13 at 10:55
  • when I move the mouse over my application –  May 06 '13 at 10:55
  • Answering my question. In what way doesn't it work? You move your mouse over your application... and what happens? What should happen? Does it not print anything? Does it crash? Does it start to smell of burning? – Joseph Mansfield May 06 '13 at 11:00
  • i should print a debug message "hi", like it can be done one pressing the mouse anywhere on my application –  May 06 '13 at 11:01
  • 2
    Did you set [mouseTracking](http://qt-project.org/doc/qt-4.8/qwidget.html#mouseTracking-prop) property to `true` ? – borisbn May 06 '13 at 11:02
  • yes i did it, {if (event->type() == QEvent::MouseMove)//not working {setMouseTracking(true); iarm->printStatus("hi"); //this is for debugging }} –  May 06 '13 at 11:06
  • 1
    Nope... you should call setMouseTracking **before** installing an event filter. Somewhere in `iArmMainWindow`'s c-tor – borisbn May 06 '13 at 11:07
  • @borisbn i think you should make it an answer – spiritwolfform May 06 '13 at 11:09
  • thanks @borisbn, put it as answer it the correct one –  May 06 '13 at 11:09
  • Ok. just a minute. Additionally I'll try to explain another way to tracking mouse (instead of installing filter) – borisbn May 06 '13 at 11:11

1 Answers1

8

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
}
borisbn
  • 4,988
  • 25
  • 42