[Qt-interest] How to solve grabmouse issue

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Wed Jul 28 16:54:38 CEST 2010


Ramesh wrote on Wednesday, July 28, 2010 3:52 PM:

> ...
> While googling, I found this
> http://stackoverflow.com/questions/2158810/passing-forwarding-events-to-a-sub-object-in-qt
> 
> I don't know how to implement this

Well then, time to get educated: http://doc.trolltech.com/4.6/eventsandfilters.html

Basically the answer on Stack Overflow suggests that your first widget gets the mouse events (button clicks, mouse movement events, ...) and creates new http://doc.trolltech.com/4.6/qmouseevent.html and send them to other widgets, as described in above links. However your first widget must map the mouse coordinates to the ones of the other widget (that is [0, 0] should be top-left of the other widget AFAIR), such that e.g. http://doc.trolltech.com/4.6/qmouseevent.html#pos returns the proper *relative* coordinate (relative to the second widget off course). Also your first widget needs to know *when* to actually create and forward these mouse events, that is when the mouse hovers over that other widget. Basically your first widget - the one which grabbed the mouse - needs to know the exact whereabout of the other widget (it needs to know the "scene graph" or the layout of the widgets, where they are placed) - basically what the event queue of Qt usually does for you(r widgets).

If this all sounds scary to you (and yes, there is some complexity behind it, especially the mapping of the coordinates) then you are probably better of by *not* grabbing the mouse in the first place - and let Qt do the proper mouse event creation ;)

Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list