简体   繁体   中英

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

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

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 .

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 )

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.

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.

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.

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);
    }
}
 

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM