tags:

views:

670

answers:

2

Okay, using Qt, I'd like to know how to detect the current state of the mouse at any point in time - without a MouseEvent.

Using QCursor::pos(), you can get its position, but is there a way to determine the current state of the buttons?

Basically, I'm looking to verify the state of the mouse when a timer goes off, so it won't be related to any particular MouseEvent, and so there's no MouseEvent to query. I need to know how to query for the mouse's state - in particular the state of the buttons - without having a MouseEvent.

Oh, and I'm using Qt 3, so if such a function has been added in Qt 4 but isn't in Qt 3, it doesn't help me much (though it would still be nice to know about).

+1  A: 

Is it a hard requirement that you don't use MouseEvent? Or can you use MouseEvents indirectly?

If you create a boolean variable for every button and update it with mouse pressed / released events then you could just look at the values of the relevant booleans when the timer goes off.

Dave
It cannot be event related. I mean, if there were a function that gave you the current mouse state by returning a MouseEvent, that would be fine, but I can't be handling an event. I have to be able to simply query for the current state of the mouse without having to worry about what events have or haven't fired.
Jonathan M Davis
Why don't you have access to the events? Are you worried about performance? How is your code getting run in a GUI without dispatch due to an event?
Bill
It's a complex system which is definitely not bug-free, and I can't guarantee that I won't miss any events. So, while it's set up so that the timer _should_ only go off when the mouse is in the correct state, I need to be able to check the mouse's state when it goes off in order to guarantee that no events fell through the cracks.
Jonathan M Davis
I appreciate the fragility of complicated systems. :) I'd recommend going with Dave's solution, but just save the flags themselves instead of converting them to booleans. Even if you drop a few events (though Qt should deliver every mouse event to you), you'll be as up-to-date as you can be until you get access to Qt4.
Bill
A: 

Qt::MouseButtons QApplication::mouseButtons () [static]:

"Returns the current state of the buttons on the mouse. The current state is updated syncronously as the event queue is emptied of events that will spontaneously change the mouse state (QEvent::MousePress and QEvent::MouseRelease events).

It should be noted this may not reflect the actual buttons held on theinput device at the time of calling but rather the mouse buttons as last reported in one of the above events. If no mouse buttons are being held Qt::NoButton is returned."

Edit: hmm, I just noticed you asked about Qt3.3. This answer applies to Qt4 I'm afraid.

Bill
Well, like I said in the question, it's still nice to know if there's a way to do it in qt4 even if there isn't in qt3. If nothing else, I could use it in a future version of the application in question (and it's just always good to know more about that sort of thing). The current version, however, is in qt3, so unless there's a way to do it in qt3, then I'm out of luck for the moment. But if it's in qt4 and not qt3, that just goes to show that trolltech continues to improve qt and make it a better toolkit.
Jonathan M Davis