PDA

View Full Version : Minimize to Tray



QT8seven
5th December 2011, 00:41
I have searched all around and cannot get this to work! Attempting to call hide() within changeEvent in a way such as:


void AppWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange)
if(isMinimized())
{
hide();
}
QMainWindow::changeEvent(event);
}

causes the window to white-out rather than hide. Does anyone know why this happens or how to fix this? I am attempting to use a tray-icon on minimize, but without being able to hide will never have much success. Currently, the only way I can work around this is to use a singleShot timer to call the hide.

Oleg
5th December 2011, 01:28
Try this way (at least it works fine in our projects):


if (event->type() == QEvent::WindowStateChange && windowState() & Qt::WindowMinimized)
QMetaObject::invokeMethod(this, "hide", Qt::QueuedConnection);

QT8seven
5th December 2011, 02:26
That's very neat, works great. Next problem:

When I restore the application it loses it's window-size. It remembers it's location, but forgets the size. Is there a proper way to restore, or am I going to have to save the settings and restore them manually when brought back from the tray. I am using:

showNormal();

I have tried simply using 'show' and 'activateWindow' but this has the same flaw and in addition it fails to bring the window to the front.

Added after 42 minutes:

Currently I have fixed the problem using saveGeometry and restoreGeometry. However, if this is a needless work-around I'd like to drop it. Any ideas are welcome.

Oleg
5th December 2011, 11:12
Maybe this will help:


void MainWindow::restoreWindow()
{
setVisible(true);
if(isMinimized())
setWindowState(windowState() & ~Qt::WindowMinimized);

#ifdef Q_OS_WIN32
::SetForegroundWindow(effectiveWinId());
::SetWindowPos(effectiveWinId(), HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
#else
activateWindow();
#endif
}