PDA

View Full Version : Strange mouseReleaseEvent behaviour.



ricardo
4th May 2009, 16:58
Hi all!

Why if I press and release left or right mouse button, I only see "release" message instead of "release left" or "release right"?



void CMyGLDrawer::mouseReleaseEvent(QMouseEvent *event) {
qDebug("release");
if (event->buttons() & Qt::LeftButton) {
qDebug("release left");
}
if (event->buttons() & Qt::RightButton) {
qDebug("release right");
}
event->accept();
}


Thanks a lot for your help ;)

spirit
4th May 2009, 17:28
try this code


void CMyGLDrawer::mouseReleaseEvent(QMouseEvent *event) {
qDebug("release");
if (event->button() == Qt::LeftButton) {
qDebug("release left");
}
if (event->button() == Qt::RightButton) {
qDebug("release right");
}
event->accept();
}

ricardo
5th May 2009, 00:14
Works fine, changing buttons by button and comparer.

By the way, if I press two buttons at the same time, and I move the mouse, how many events will I receive? 1 or 2?

Thanks a lot.

spirit
5th May 2009, 06:06
your code will work in this case, i.e. if you pressed two or more mouse buttons then QMouseEvent::buttons will retrun all pressed buttons, that's why this function doesn't work for one button.

wysota
5th May 2009, 09:22
your code will work in this case, i.e. if you pressed two or more mouse buttons then QMouseEvent::buttons will retrun all pressed buttons, that's why this function doesn't work for one button.

I think that it doesn't work because it returns the current state of the buttons and so in release event no buttons are in the pressed state while in mouse move they are. So the same code put in mouseMove will work while it doesn't with mouseRelease. button() on the other hand returns the button that caused the event (regardless of its current state).

spirit
5th May 2009, 09:28
I talked about CMyGLDrawer::mouseReleaseEvent and QMouseEvent::buttons not about moveEvent. :)

ricardo
5th May 2009, 10:30
your code will work in this case, i.e. if you pressed two or more mouse buttons then QMouseEvent::buttons will retrun all pressed buttons, that's why this function doesn't work for one button.

Wait. Still don't understand.
Which case? Mine or Spirit?

if I press two buttons at the same time, and I release them at the same time, how many event would I receive? 1 or 2?

I think moving the mouse I will receive only 1, so I have to use:
(event->buttons() & Qt::LeftButton) and (event->buttons() & Qt::RightButton)

But not on release and press, as I guess I will only receive 1. So I have to use:
(event->button() == Qt::LeftButton) or (event->button() == Qt::RightButton)

Am I right?

Thanks a lot for your help and time.

spirit
5th May 2009, 10:36
Wait. Still don't understand.
Which case? Mine or Spirit?

I was talking about code snippent wich you posted.



if I press two buttons at the same time, and I release them at the same time, how many event would I receive? 1 or 2?

in that case you receive one event, but QMouseEvent::buttons will return two buttons.

but if you need to determinate wich buttons have been pressed while moving a mouse then you need to use wysota's suggestion.

ricardo
5th May 2009, 10:48
Understood.
Thanks a lot.

wysota
5th May 2009, 20:57
if I press two buttons at the same time, and I release them at the same time, how many event would I receive? 1 or 2?

Most likely four - two press and two release events.

I'm currently experimenting with xev and it seems on X11 you would get three events - one press and two release.

ricardo
5th May 2009, 23:28
Most likely four - two press and two release events.

I'm currently experimenting with xev and it seems on X11 you would get three events - one press and two release.

Really? One press event when you press 2 buttons??? Sounds strange, doesnt it?

wysota
5th May 2009, 23:35
Really? One press event when you press 2 buttons??? Sounds strange, doesnt it?

No, actually I was wrong.


ButtonPress event, serial 31, synthetic NO, window 0x5800001,
root 0x13c, subw 0x5800002, time 36532044, (48,35), root:(735,101),
state 0x0, button 1, same_screen YES

EnterNotify event, serial 31, synthetic NO, window 0x5800001,
root 0x13c, subw 0x0, time 36532044, (48,35), root:(735,101),
mode NotifyGrab, detail NotifyInferior, same_screen YES,
focus YES, state 256

KeymapNotify event, serial 31, synthetic NO, window 0x0,
keys: 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

ButtonPress event, serial 31, synthetic NO, window 0x5800001,
root 0x13c, subw 0x5800002, time 36532068, (48,35), root:(735,101),
state 0x100, button 3, same_screen YES

ButtonRelease event, serial 31, synthetic NO, window 0x5800001,
root 0x13c, subw 0x5800002, time 36537716, (48,35), root:(735,101),
state 0x500, button 3, same_screen YES

ButtonRelease event, serial 31, synthetic NO, window 0x5800001,
root 0x13c, subw 0x5800002, time 36537740, (48,35), root:(735,101),
state 0x100, button 1, same_screen YES

You can see two ButtonPush events reported, once for the left (1) and then for the right (3) mouse button.