1

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

Alexander V
  • 8,351
  • 4
  • 38
  • 47
ANIRUDH NJ
  • 59
  • 2
  • 8
  • The code you provided is not even compilable. Unsure what is going on then when the return value is not set. I only made it more readable but did not modify. – Alexander V Apr 07 '16 at 06:25
  • Sorry, i didnt get you. This is the code only for a widget class event filter methodother functions are not given. I think the only changes that are required for the mouse event to work in both the _QGraphicView_ and _button_ are in the event filter and the constructor of Widget class. – ANIRUDH NJ Apr 07 '16 at 07:24
  • bool Widget::eventFilter supposed to return either true or false. And that is important and not very precise Qt version here. It is better to overload Widget::event function I guess. Read on that. also event->accept and ignore is important. – Alexander V Apr 07 '16 at 14:54

0 Answers0