打开应用程序主窗口在systray图标的其他应用程序之上

时间:2017-06-29 11:37:14

标签: c++ windows qt

Qt应用最小化。 在QML上实现UI,ApplicationWindow为主窗口。

当我右键单击托盘中的应用程序图标时,会显示一个菜单。其中一个菜单项是" Open"。单击它时,将调用此代码(很可能是冗余的):

mainWidget->activateWindow();
window->showNormal();
Qt::WindowState newWindowState = (Qt::WindowState)((window->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
window->setWindowState(newWindowState);

如果双击托盘图标时调用相同的代码,则应用程序窗口会打开,但仍保留在其他应用程序下。

如何解决这个问题?

qDebug() << "GetCurrentThreadId: " << GetCurrentThreadId();
qDebug() << "windowFlags: " << mainWidget->windowFlags();
qDebug() << "windowState: " << window->windowState();

mainWidget->activateWindow();
window->showNormal();
Qt::WindowState newWindowState = (Qt::WindowState)((mainWindow()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
window->setWindowState(newWindowState);

qDebug() << "GetCurrentThreadId: " << GetCurrentThreadId();
qDebug() << "windowFlags: " << mainWidget->windowFlags();
qDebug() << "windowState: " << window->windowState();

成功打开应用程序时的输出:

[16:00:17]  [D]  GetCurrentThreadId:  1092
[16:00:17]  [D]  windowFlags:  QFlags<Qt::WindowType>(Popup|WindowTitleHint|WindowSystemMenuHint|WindowMinMaxButtonsHint|WindowCloseButtonHint|WindowFullscreenButtonHint)
[16:00:17]  [D]  windowState:  Qt::WindowState(WindowMinimized)
[16:00:17]  [W]  QWindow::setWindowState does not accept Qt::WindowActive
[16:00:17]  [D]  GetCurrentThreadId:  1092
[16:00:17]  [D]  windowFlags:  QFlags<Qt::WindowType>(Popup|WindowTitleHint|WindowSystemMenuHint|WindowMinMaxButtonsHint|WindowCloseButtonHint|WindowFullscreenButtonHint)
[16:00:17]  [D]  windowState:  Qt::WindowState(WindowNoState)

当窗口保持在其他应用程序下时输出:

[16:00:23]  [D]  GetCurrentThreadId:  1092
[16:00:23]  [D]  windowFlags:  QFlags<Qt::WindowType>(Popup|WindowTitleHint|WindowSystemMenuHint|WindowMinMaxButtonsHint|WindowCloseButtonHint|WindowFullscreenButtonHint)
[16:00:23]  [D]  windowState:  Qt::WindowState(WindowMinimized)
[16:00:23]  [W]  QWindow::setWindowState does not accept Qt::WindowActive
[16:00:23]  [D]  GetCurrentThreadId:  1092
[16:00:23]  [D]  windowFlags:  QFlags<Qt::WindowType>(Popup|WindowTitleHint|WindowSystemMenuHint|WindowMinMaxButtonsHint|WindowCloseButtonHint|WindowFullscreenButtonHint)
[16:00:23]  [D]  windowState:  Qt::WindowState(WindowNoState)

1 个答案:

答案 0 :(得分:0)

找到了一个有效的解决方案 https://stackoverflow.com/a/41565553/3569069

QWindow* mainWindow = qApp->topLevelWindows()[0];
mainWindow->show();
mainWindow->raise();
mainWindow->requestActivate();

编辑1.上述方法对Windows的行为有一些小问题。最后,它是这样做的:

docker run -it k0pernikus/httpie-docker-alpine HEAD https://stackoverflow.com/