Qt mouse move events not caught by an event filter

半世苍凉 提交于 2020-01-02 05:25:14

问题


I can't seem to catch QEvent::MouseMove typed events in my eventFilter.

Here's my event filter:

bool
MapWidget_c::eventFilter( QObject *obj, QEvent *ev )
{
   if( obj == graphicsGeoMap_mp ||
       obj == graphicsScene_mp ||
       obj == graphicsView_mp )
   {
      if( ev->type() == QEvent::MouseMove )
      {
         QMouseEvent *mouseEvent = static_cast< QMouseEvent* >( ev );

         mouseMoveEvent( mouseEvent );

         return true;
      }
      else
      {
         return false;
      }
   }
   else
   {
      // pass the event on to the parent class
      return QWidget::eventFilter( obj, ev );
   }
}

I install the filters like this:

graphicsGeoMap_mp->installEventFilter( this ); //QGraphicsGeoMap
graphicsScene_mp->installEventFilter( this ); //QGraphicsScene
graphicsView_mp->installEventFilter( this ); //QGraphicsScene

The event filter seems to catch mousePress and mouseRelease events just fine, but not mouseMove.

What could be the problem?


回答1:


It turns out that I was looking for wrong kind of mouseMove events.

I should've been catching QEvent::GraphicsSceneMouseMove events instead of QEvent::MouseMove events.




回答2:


Mouse move events are not generally enabled. You need to enable mouse tracking (via setMouseTracking) on your wigdet(s) to get them.

From QMouseEvent:

Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with QWidget::setMouseTracking().



来源:https://stackoverflow.com/questions/6439681/qt-mouse-move-events-not-caught-by-an-event-filter

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