[Qt-interest] setMouseTracking
Nikos Chantziaras
realnc at arcor.de
Fri Jan 7 23:54:59 CET 2011
On 01/07/2011 11:18 PM, william.crocker at analog.com wrote:
>
>
> Nikos Chantziaras wrote:
>> On 01/07/2011 04:36 PM, "Alexander Carôt" wrote:
>>>>>> Sorry, next idea .. .-)
>>>> Query QCursor::pos() on a timer?
>>> Just tried it and it couldn't be simpler than that ;-)
>>> What you propose exactly does what I need !
>>>
>>> So, basically no need to use the mouseHandlers and setMouseTracking
>>> options etc..
>>>
>>> Right on and thanks a lot to everyone (and esp. Bill for the final idea)
>>
>> Of course you should know that this makes your application run code
>> all the time, waking up the CPU even when idle. Users are known to
>> throw darts at pictures of the developer for stuff like this :-)
>>
>
> I agree.
> You should query at the lowest acceptable frequency.
> You could then scale the frequency up and down with
> the apparent mouse velocity.
I think the best approach to this is to simply write a custom event
filter and install it on the global application pointer. And here's how
it's done:
First, create a class that inherits from QObject and provides a
protected bool eventFilter(QObject*, QEvent*) function:
class MouseFilter: public QObject {
protected:
bool eventFilter(QObject* obj, QEvent* event);
};
In that function, you check whether the event is a mouse move:
bool MouseFilter::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::MouseMove) {
// It's a mouse move. Do whatever you need to do here.
}
// Do with the event whatever the default was.
return QObject::eventFilter(obj, event);
}
(If you wish to suppress the event, return false instead.)
Now install the event handler on qApp, probably in main():
MouseFilter filter;
qApp->installEventFilter(&filter);
And that's it. No need for ugly timer polling.
More information about the Qt-interest-old
mailing list