Bring window to front -> raise(),show(),activateWindow() don’t work
Solution 1
This problem is specific to Windows. If the active window belongs to some process, then Windows does not allow other processes to change the active Window.
(Do not try the following: https://wiki.qt.io/Qt_project_org_faq#QWidget_::activateWindow.28.29_-_behavior_under_windows)
Solution 2
try this:
viewer.setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
viewer.raise(); // for MacOS
viewer.activateWindow(); // for Windows
it work in my project ( in my project viewer is QMainWindow): https://github.com/iptton/Rythem .
Solution 3
I did it like this:
{
this->show(); // Restore from systray
this->setWindowState(Qt::WindowState::WindowActive); // Bring window to foreground
}
assuming "this
" is your QMainWindow
.
Worked like a charm.
Solution 4
for ( QWindow* appWindow : qApplication.allWindows() )
{
appWindow->show(); //bring window to top on OSX
appWindow->raise(); //bring window from minimized state on OSX
appWindow->requestActivate(); //bring window to front/unminimize on windows
}
Note that this also brings up the window from other virtual desktops on both OSX and Windows. I did not test this on linux, it may work though.
Solution 5
This issue is not specific to Windows....I have the same issue on Linux. My solution was to close() the window before I re open() it.
Hedge
Updated on July 09, 2022Comments
-
Hedge almost 2 years
In my Qt-application I open a URL in the default-browser. Afterwards I want to bring the main-window of my application to the front again.
I tried all approaches I could find but none worked. All it does is blink in the taskbar (of Window 7) Here’s an example:
this->viewer->show(); this->viewer->raise(); this->viewer->activateWindow();
*viewer is a pointer to a QmlApplicationViewer which is derived from QDeclarativeView
-
Hedge about 13 yearsThanks for the explanation. Your approach is pretty hacky. As I'm a die-hard portable software advocate I don't want to fiddle with the registry of someone's PC. Is there another approach like setting the window topmost?
-
Adrian McCarthy about 13 yearsThis isn't a Windows-specific problem. It's a solution to all the crappy, focus-stealing applications. Windows signals that the application wants the user's attention, but it leaves the user in charge.
-
Cody Gray about 13 yearsDoesn't matter what color hat you wear: that only "fixes" it on your local machine, not the client's. Thanks to UAC, also introduced in later versions of Windows, my registry settings are safely off-limits to your rogue application!
-
mlvljr about 10 yearsactivateWindow() works nicely for me under win'7 with 4.8.6 :)
-
ManuelSchneid3r over 8 years"The enum value
Qt::WindowActive
is not an accepted parameter". From the QWindow::setWindowState docs. -
iptton over 7 yearsfossies.org/diffs/qt-everywhere-opensource-src/5.6.1_vs_5.7.0/… as the source code mentioned: when the parameter equals to Qt:WindowActive , setWindowState will return directly. And the raise() activityWindow() works for this situation. But if the state of window isn't Minimized ,the state is not equals to Qt::WindowActivie . And that's why the code works.
-
c z about 7 yearsI get the same problem on MacOS
-
pier_nasos almost 7 yearsThank you very much. This is the only way that works fine in all my cases.
-
smoothware over 6 yearsThis worked for me on Win10. All I needed was the setWindowState (combined with show() to restore app from tray) thanks!
-
iMath about 5 years@RuiBotelho If you minimize your window , then your method cannot bring your window to front .
-
Alex Seceleanu almost 3 yearsGreat answer, unfortunately it is not good news :))
-
Thalia over 2 yearsFunny behavior - trying to bring the app in front - it stays stubbornly behind but the icon on taskbar blinks for attention