qt mouse event filter
Asked Answered
W

0

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

Whelp answered 7/4, 2016 at 3:55 Comment(3)
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.Rudyrudyard
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.Whelp
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.Rudyrudyard

© 2022 - 2024 — McMap. All rights reserved.