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

7

35

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

Sustainer answered 22/5, 2011 at 12:22 Comment(10)
The docs are quite clear that this is the expected behaviour. Of course you can avoid the issue entirely by integrating any web browsing into your application using QWebKit.Ariel
Thanks for the hint but I compiled my own libraries without QtWebKit and don't want to use it.Sustainer
Why do you want to steal focus from the user's control?Achaean
because it get stolen automatically by the browser which is opened by my application. I just want to bring my window up again after he allowed access for my application on Twitter.Sustainer
The exact rules are spelled out in the MSDN Library article for SetForegroundWindow().Proof
Thanks to Z order, your application will automatically come back up again once the user closes the browser window. It's the next window in the Z order underneath the newly-opened browser window. You don't have to do anything at all.Insurgent
@Code Gray: Yeah that's correct, but I'd rather put it in front. The browser opens in front as well so where's the difference?Sustainer
So everyone complains but no one wants to give an answer? I have the same problem. When users press on settings a new window opens but if they don't observe it and press again on settings the settings window will stay in back, I want to say "hey, I'm here, here, I popped back in front for you". A solution would be to make setVisibile(false) and then setVisible(true) but I don't like this solution, it should just pop up back in front without closing and opening it again.Boozer
Ok, raise() worked for my problem.Boozer
I have used QDialog and I have checked as Modal on properties window and it worked.Variation
L
13

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)

Laina answered 22/5, 2011 at 12:29 Comment(6)
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?Sustainer
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.Achaean
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!Insurgent
activateWindow() works nicely for me under win'7 with 4.8.6 :)Fluttery
I get the same problem on MacOSRoyal
Great answer, unfortunately it is not good news :))Sestina
D
42

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 .

Diplegia answered 30/5, 2012 at 1:33 Comment(4)
"The enum value Qt::WindowActive is not an accepted parameter". From the QWindow::setWindowState docs.Devout
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.Diplegia
This worked for me on Win10. All I needed was the setWindowState (combined with show() to restore app from tray) thanks!Eyespot
@RuiBotelho If you minimize your window , then your method cannot bring your window to front .Ferullo
L
13

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)

Laina answered 22/5, 2011 at 12:29 Comment(6)
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?Sustainer
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.Achaean
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!Insurgent
activateWindow() works nicely for me under win'7 with 4.8.6 :)Fluttery
I get the same problem on MacOSRoyal
Great answer, unfortunately it is not good news :))Sestina
J
10

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.

Jackqueline answered 4/2, 2020 at 17:56 Comment(0)
R
3
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.

Recalescence answered 10/1, 2017 at 9:37 Comment(1)
Thank you very much. This is the only way that works fine in all my cases.Selfforgetful
M
2

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.

Madly answered 8/10, 2013 at 16:52 Comment(0)
I
0

For Windows I did it with WinAPI. Also you need to know the window title;

#include <windows.h>
const QString windowTitle = "Some title";

HWND hwnd = ::FindWindowA(NULL, windowTitle.toLocal8Bit());
if (hwnd != NULL) {
    if (::IsWindowVisible(hwnd)) {
        ::SwitchToThisWindow(hwnd, TRUE);
    }
}
 
Ikhnaton answered 19/1, 2021 at 6:3 Comment(1)
Funny behavior - trying to bring the app in front - it stays stubbornly behind but the icon on taskbar blinks for attentionElda
F
0

The following is borrowed from the forum and it works for me:

auto eFlags = viewer.windowFlags();
viewer.setWindowFlags(eFlags|Qt::WindowStaysOnTopHint);
viewer.show();
viewer.setWindowFlags(eFlags);
viewer.show();
Farmelo answered 30/10, 2021 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.