Question

I am trying to do simly functionality. When I click on tray icon:

 -A- if app is hide > show
 -B- if app is show and is NOT focus > focus it
 -C- if app is show and focus > hide

Very simle but if you have focus app and you click on try icon, the focus is lost and now the taskbar is focused. So the B condition is true instead of C. So what with that?

Était-ce utile?

La solution

You can assume C when the click event on the tray icon occurs very shortly after the focus out event of the main window. So you could do something like this:

MainWindow::focusOutEvent(...) {
    trayIcon->setJustFocussedOut(true);
    this->someQTimer->start();
}

MainWindow::focusOutDone() {
    trayIcon->setJustFocussedOut(false);
}

Where MainWindow::someQTimer is a QTimer set to singleshot mode and a very short interval (e.g. 50 milliseconds). The setJustFocussedOut method sets a flag in the tray icon which makes it handle a click as case C.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top