【问题标题】:Qt mouse move events not caught by an event filter事件过滤器未捕获 Qt 鼠标移动事件
【发布时间】:2011-06-22 12:18:54
【问题描述】:

我似乎无法在我的 eventFilter 中捕获 QEvent::MouseMove 类型的事件。

这是我的事件过滤器:

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 );
   }
}

我这样安装过滤器:

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

事件过滤器似乎可以很好地捕获 mousePress 和 mouseRelease 事件,但不能捕获 mouseMove。

可能是什么问题?

【问题讨论】:

    标签: qt events qevent


    【解决方案1】:

    原来我在寻找错误的 mouseMove 事件。

    我应该一直在捕捉 QEvent::GraphicsSceneMouseMove 事件而不是 QEvent::MouseMove 事件。

    【讨论】:

      【解决方案2】:

      鼠标移动事件通常不启用。您需要在您的 wigdet(s) 上启用鼠标跟踪(通过 setMouseTracking)才能获取它们。

      来自QMouseEvent

      只有在按下鼠标按钮时才会发生鼠标移动事件,除非使用 QWidget::setMouseTracking() 启用了鼠标跟踪。

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多