[Interest] Place a QWidget below a button (and follow it!)

Andre Somers andre at familiesomers.nl
Thu Jun 13 20:38:47 CEST 2013


Op 13-6-2013 19:37, william.crocker at analog.com schreef:
>> In details, I have a widget that will display a floating window when the
>> user clicks on a QToolButton. This floating widget must be placed below
>> the button that summoned its soul, and if the main window moves, the
>> floating window must be positioned in the same place.
>>
> Reimplement the moveEvent of the main window
> (which has the tool button as a child.)
> In it, reevaluate the position of the child
> tool button (to be tracked) and move the
> floating window as required.
>
> Converting widget positions in and out of the
> global coordinate system will come in handy.
While Bill is right, there is another way that allows you to do this 
without changing the main window for it: event filters. That will allow 
you to keep all the code you need for this in one place, in the widget 
that you want to make move.

What I'd do, is give that widget a constructor argument with a pointer 
to the widget to follow around. Using QWidget::window, you find out what 
the window is the widget to be followed lives in. Install an event 
filter on that window-widget. If you use scrollable views somewhere, 
you'll need to iterate up yourself, to install the event filter for 
every widget in between too.
In your widget-that-follows, reimplement the eventFilter(QObject*, 
QEvent*) method, and look for move and resize events. If you get these, 
you again take the geometry of the widget-to-follow, and use the 
mapToGlobal function of the widget-to-follow to get the global 
coordinates. Then, re-position your widget-that-follows to those 
coordinates.

André




More information about the Interest mailing list