Bring window to front -> raise(),show(),activateWindow() don’t work

41,174

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.

Share:
41,174
Hedge
Author by

Hedge

Updated on July 09, 2022

Comments

  • Hedge
    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
    Hedge about 13 years
    Thanks 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
    Adrian McCarthy about 13 years
    This 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
    Cody Gray about 13 years
    Doesn'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
    mlvljr about 10 years
    activateWindow() works nicely for me under win'7 with 4.8.6 :)
  • ManuelSchneid3r
    ManuelSchneid3r over 8 years
    "The enum value Qt::WindowActive is not an accepted parameter". From the QWindow::setWindowState docs.
  • iptton
    iptton over 7 years
    fossies.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
    c z about 7 years
    I get the same problem on MacOS
  • pier_nasos
    pier_nasos almost 7 years
    Thank you very much. This is the only way that works fine in all my cases.
  • smoothware
    smoothware over 6 years
    This worked for me on Win10. All I needed was the setWindowState (combined with show() to restore app from tray) thanks!
  • iMath
    iMath about 5 years
    @RuiBotelho If you minimize your window , then your method cannot bring your window to front .
  • Alex Seceleanu
    Alex Seceleanu almost 3 years
    Great answer, unfortunately it is not good news :))
  • Thalia
    Thalia over 2 years
    Funny behavior - trying to bring the app in front - it stays stubbornly behind but the icon on taskbar blinks for attention