Is there a way to keep a widget on top of other windows without stealing focus?
I'm writing an application for OSX/Windows that will basically hover as a small box on top of other applications without stealing focus (but can be interacted with). Using
webview->setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog);
And a loop that constantly calls
webview->raise()once per second seems to work well on windows. If the user uses something like powerpoint, the slides will work without hiding the tool or stealing focus from the slides.
The loop sort of looks like this:
QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(goTop())); timer->start(1000);
However, on OSX the
raise()function steals focus from all the other applications. This is very annoying. Without the loop, the tool gets blocked out by other services trying to go full screen (like LibreOffice presentation slides).
I know NSWindow has
makeKeyAndOrderFront(_:), which brings the window to the front, but doesn't focus it. Is there some something similar implemented with QT 5? Is this a potential ethical problem as well?
Any help is appreciated.
The point of using Qt::WindowStaysOnTopHint is so that it will stay on top, without the need to call raise() periodically.
Does that flag alone not work for you?
@sierdzio Hi, thank you for responding! Yes that flag works on its own when placing the view over say Microsoft PowerPoint slides, but does not work when the widget is floating over Keynote slides (this is my main use case). My feeling is that I'm basically fighting an "on top" war with the other application, which may be using its own native version of "on top".
Ah I see, that looks likely.
As far as I know, Qt does not offer a wrapper around makeKeyAndOrderFront(), but nothing is stopping you from using it. A bit of platform-dependent code is not a crime :-)
Also, since you have found that raise() causes different behaviour on Windows and Mac, please consider reporting it as a bug to Qt bugtracker.
@sierdzio Thanks for the reply. Any chance you could point me in the right direction for how to access the underlying native view. Does QT expose that in some way?
Also, another big difference appears to be in how
Qt::WindowStaysOnTopHinttreats full screen browsers on Windows versus OSX. On windows, the widget stays on top of full screened youtube and other pages. On OSX, the widget is pushed out of the way. If this kind of behaviour difference warrants a bug report, I can file that as well.
I rarely code on Macs so I can't help you with anything specific.
Some things that might point you in the right direction:
- try setting your widget's window flags to Qt::Tool
- take a look at effectiveWinId()
- check out QMacNativeWidget
- and lastly, maybe Mac extras module has some helpers for that: link
About the bug report: in my view, this is indeed a bug worth reporting. However, since Mac window manager is so unusual, maybe this behaviour is simply inavoidable/ intentional. If so, at least the docs should be updated to warn people about it and suggest workarounds.