Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Keep windows on top within application
I'm trying to set up my app to keep certain main windows above other main windows, when any of my app's windows have focus. I believe this behavior I'm trying to get is similar to WS_EX_PALETTEWINDOW (or WS_EX_TOPMOST) on Windows.
I've tried using Qt::WindowStaysOnTopHint however this causes the windows to stay above all other windows, even when my app is not in focus. I've also tried setting/unsetting Qt::WindowStaysOnTopHint when the app goes in/out of focus (or manually setting HWND_TOPMOST) but this causes issues with windows getting reordered (not nice for Alt+Tab'ing back to the app).
Finally, I've also had a go using window parenting. This works nicely for one parent window, however my situation means I have more than one window on the bottom layer, and more than one window that must stay on top.
Is there a special flag for this that I'm missing? Anyone solved this problem before? Any advice is appreciated.
IamSumit last edited by
and welcome to Qt devnet
Have you tried this ->void QWidget::raise () [slot]
gvanvoor last edited by
On Mac, you can use the Qt::Tool flag for this, but that doesn't result in this effect on Windows. As suggested, a workaround would consist of raising those windows whenever one that should stay below has been activated.
Thanks for the feedback guys, very much appreciated. Looking at source, it doesn't seem like there's a window flag that maps to WS_EX_PALETTEWINDOW. I'll try using QWidget::raise() and seeing if that can solve the issue.
Just a quick update for anyone interested. I wasn't able to get the behavior I wanted to work correctly with raise(). I ended up using:
@bool isOnTop = static_cast<QGuiApplication*>(qApp)->applicationState() == Qt::ApplicationActive;
SetWindowPos(handle, isOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
This has to be applied to the windows that need to be on top, in the reverse order they are stacked in.
The only issue with doing this is that it caused the alt+tab order to be wrong, so I settled with making my always-on-top windows use the Qt::Tool window flags -- this causes them to not appear in the alt+tab list.