[Interest] Find frontmost widget of specific type?

Henry Skoglund fromqt at tungware.se
Mon Oct 22 23:22:24 CEST 2018

On 22/10/2018 22.37, John Weeks wrote:
> We faced pretty much exactly this issue when we ported our very large application to Qt, starting with Qt 4.8. We have many places where we expect to be able to walk a window list in Z order. I wound up using Activate/Deactivate events to keep the list myself. I can't really recommend it- it has been pretty much of a nightmare to make it robust and bug-free, especially as Qt has a couple of bugs in their own notion of window activation. You can't really use the debugger to debug these issues, as the activation of the debugger changes the activation of the application's windows.
> I have made it work pretty well, but I quake in my boots whenever I get a bug report about window order.
> We are now using Qt 5.9 and don't have any sort of replacement for my delicate and difficult code.
> -John Weeks
> WaveMetrics, Inc.
>> On Oct 22, 2018, at 11:37 AM, Israel Brewster <ibrewster at flyravn.com> wrote:
>> I have an application (Qt 5.9) that has a variety of different types of windows you can open. If a user selects to open a type of window that is already open, I want to position the new  window relative to the existing one. I can easily find any existing windows of a given type by going through the list of widgets in QApplication::allWidgets(), doing a qobject_cast to the proper type, and checking the result, but is there a way to determine which of these is the frontmost of that type? QApplication::ActiveWindow() doesn't help, because the activeWindow may not be of that type.

Hi, just an idea, but obviously somewhere deep inside Qt's bowels 
(rather, the painting system) there's knowledge of which window obscures 
which etc.

Say you issue a dummy QWidget::update() on your topmost/app window, then 
catches all the QPaintEvents that are called. Inside those QPaintEvents 
is a QPaintEvent::rect() that holds the rectangle that needs to be 
updated/repainted. If you then search your QApplication::allWidgets()'s 
list of rectangles; then I think (guessing) that only the 
topmost/foremost windows will match QPaintEvent's rectangles. I.e. 
windows that are partially or fully obscured by another window should 
receive a smaller (or none) rectangle in those QPaintEvents. That could 
be one way to establish a z-order on the fly...

Rgrds Henry

More information about the Interest mailing list