1

Qt application minimized. UI implemented on QML with ApplicationWindow as mainwindow.

When I right-click on the application icon in the tray, a menu is displayed. One of the menu items is "Open". When click on it, this code is called (most likely, redundant):

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

If the same code is called when double-click on the tray icon, the application window opens, but remains under the other applications.

How can this be remedied?

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

Output when the application is successfully opened:

[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)

Output when the window remains under other applications:

[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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
pier_nasos
  • 668
  • 4
  • 17
  • Can you check the value of newWindowState with your debugger in both cases? – KjMag Jun 29 '17 at 12:50
  • I know that you've already found the solution, but if you check it and are interested why this previous approach doesn't work, I may have an interesting answer ;) – KjMag Jun 29 '17 at 12:51
  • I got the same result in both cases (added to description). It's very interesting what you hide ) – pier_nasos Jun 29 '17 at 13:11

1 Answers1

0

Found a working solution https://stackoverflow.com/a/41565553/3569069

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
}

edit 1. The approach described above has minor problems with the behavior of windows. In the end, it's done like this:

QWindow* mainWindow = qApp->topLevelWindows()[0];
mainWindow->show();
mainWindow->raise();
mainWindow->requestActivate();
pier_nasos
  • 668
  • 4
  • 17