qt mouse event filter

5.8k Views Asked by At

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

0

There are 0 best solutions below