QMainWindow, Alt Tab Behavior
-
When the user presses alt tab, i would like the application to minimize itself to the system tray. Instead what happens is the window remains open and moves to the back.
How can I handle this within Qt itself? I have a separate thread running which can implement a hot-fix if need be. How can I identify this state, so as to call "showWindowMinimized()" on my QMainWindow? I did succeed in implementing a partial solution, the problem is it incorrectly fires when trying to open the window, which prevents the window from being open on certain test systems depending on the windows behavior.
-
You can use QWidget::focusOutEvent() (the reason will be Qt::ActiveWindowFocusReason), just make sure your focusPolicy is not Qt::NoFocus, to detect if the QMainWindow is no longer the active window.
If you want to know if you have been specifically focused-out by Alt+Tab you will have to use platform-specific code.
-
Alt-Tab is usually handled by the system before it ever reaches the application.
You might be able to minimize once the application uses focus or something. Not sure whether that is the beaviour you want though.
-
@ Tobias
Yes i want to minimize once the application loses focus. But again, how do i get that event.
-
@ Lukas
No, I don't get a QWidget::focusOutEvent() when I press alt tab. I did set the focus policy to Qt::WheelFocus, which is the strongest of them all, and I am still not getting the event.
-
You should. Are you sure that your event handler has the correct signature (and you are not declaring a new method instead of overriding it)?
@
void MainWindow::focusOutEvent(QFocusEvent *event)
{
if (event->reason() == Qt::ActiveWindowFocusReason)
...return QMainWindow::focusOutEvent(event);
}
@ -
Thanks a lot, I finally got it working. Had implemented a temporary work around, and then forgot about it since it wasn't top priority.
The problem was the event was going to the QDeclarativeView, not to the QMainWindow. When I added the code to the underlying view class it worked perfectly.
Thanks,
Wargamer