问题
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