qt mouse event filter

天涯浪子 提交于 2020-01-05 16:48:33

问题


I have a QWidget with a QGraphicsView and a push button. The QGraphicsView has to take mouse press and release events to detect a swipe .At the same time push button should run a small function on clicked. I used an event filter in the QWidget to detect the mouse events.

bool Widget::eventFilter(QObject * obj, QEvent * event)
{
  // Capturing keyboard events for moving
  if( event->type() == QEvent::KeyPress )
  {
     //Do something
  }
  //Capturing mouse events for swipe
  else if( event->type() == QEvent::MouseButtonPress)
  {
     QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
     swipe_startPoint = mouseEvent->pos();
  }
  else if( event->type() == QEvent::MouseButtonRelease)
  {
    QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
    swipe_endPoint = mouseEvent->pos();
    swipeDirection();
  }
  else
  {
     QWidget::eventFilter(obj,event);
  }
}

In the constructor of the Widget class i have the following

ui->graphicsView->installEventFilter(this);

The problem is that the button is getting clicked but the MouseButtonRelease event that sets the 'swipe_endPoint' value is not working.

When i set

ui->graphicsView->grabMouse();

the mouse pressed and released events are working perfectly but the button stops accepting the events.

Other things that i have tried are :-

Set the event filter to the viewPort

ui->graphicView->viewPort()->installEventFilter(this);

Please help me get this mouse event working. Thanks in advance.

Note : Using QT 5.6.0 in Ubuntu

来源:https://stackoverflow.com/questions/36466415/qt-mouse-event-filter

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