PDA

View Full Version : Problem in Mouse Events



anirudh123
6th April 2016, 12:10
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.

Note : Using QT 5.6.0 in Ubuntu

Can you please help me overcome this problem.
Thanks in advance :D

anda_skoa
6th April 2016, 12:53
Try installing the event filter in the graphicsview's viewport widget.

Cheers,
_

anirudh123
6th April 2016, 16:27
Thanks a lot for your input . I replace the code as following
ui->graphicsView->viewport()->installEventFilter(this); . After this the graphicView stopped taking mouse events :(

anda_skoa
6th April 2016, 17:34
Are you returning false in your event filter when you are not handling the event?

Cheers,
_

anirudh123
7th April 2016, 05:35
No, where shall i do that?

In my code if the event is not taken it goes to the else block of the eventFilter() and the event is passed to QWidget::eventFilter(obj,event);

anda_skoa
7th April 2016, 09:30
No, where shall i do that?

The eventFilter() method needs to return either true or false, depending on whether it has consumed the event or not.



In my code if the event is not taken it goes to the else block of the eventFilter() and the event is passed to QWidget::eventFilter(obj,event);

Maybe you could post the actual code?
The snippet you posted earlier does not return anything in any of the other branches. If that results in eventFilter() being evaluated as true, then mouse press/release and key press never make it to the actual target.

Cheers,
_