Minimizing Application to Tray
-
I had the same problem with a windowless device... Decided for this solution :)
-
To remove the button is no solution...
Here is a dirty hack, but it works.@void MainWindow::changeEvent(QEvent *e)
{
if(isMinimized())
{
show(); // The hack
slotToggleVisibility(); // Your tray icon code
}
}@ -
If anyone still has this issue, the solution is to call qApp->processEvents() before calling hide(), ie.
@void ApplicationWindow::changeEvent(QEvent *event)
{
QWidget::changeEvent(event);
if (event->type() == QEvent::WindowStateChange)
{
QWindowStateChangeEvent e = (QWindowStateChangeEvent)event;
// make sure we only do this for minimize events
if ((e->oldState() != Qt::WindowMinimized) && isMinimized())
{
qApp->processEvents();
hide();
event->ignore();
}
}
}
@The test for the old state is probably unnecessary, but can't hurt.
If this doesn't help in your particular situation, try calling hide from a single-shot timer, so that the call is outside the minimize event handler:
@void ApplicationWindow::changeEvent(QEvent *event)
{
QWidget::changeEvent(event);
if (event->type() == QEvent::WindowStateChange)
{
QWindowStateChangeEvent e = (QWindowStateChangeEvent)event;
// make sure we only do this for minimize events
if ((e->oldState() != Qt::WindowMinimized) && isMinimized())
{
QTimer::singleShot(0, this, SLOT(hide()));
event->ignore();
}
}
}
@ -
Hi Ryan,
take care if you call qApp->processEvents(), as you reopen the event queue and it might happen that an event, that should be worked on after you finished is processed now. I don't say never do it, but do it with care and always remember, synchronous workflow is broken up here.
Even a deleteLater might be processed so an object can be deleted, where you expect it not to be deleted.
-
Yep, that's why I gave the second block of code (which I've just realised had the processEvents() call still in it - it wasn't meant to be there)