When the last window closes if there is no system tray close the app

This commit is contained in:
Andrew Hayzen 2021-03-02 22:48:58 +00:00
parent 2080784dd1
commit 00565fa7ea
3 changed files with 18 additions and 2 deletions

View File

@ -29,6 +29,12 @@ QBarrierApplication::QBarrierApplication(int& argc, char** argv) :
m_Translator(NULL) m_Translator(NULL)
{ {
s_Instance = this; s_Instance = this;
// By default do not quit when the last window is closed as we minimise
// to the system tray, but listen for the lastWindow closing so that
// if the system tray is not available we can quit.
setQuitOnLastWindowClosed(false);
connect(this, &QApplication::lastWindowClosed, this, &QBarrierApplication::onLastWindowClosed);
} }
QBarrierApplication::~QBarrierApplication() QBarrierApplication::~QBarrierApplication()
@ -69,3 +75,12 @@ void QBarrierApplication::setTranslator(QTranslator* translator)
m_Translator = translator; m_Translator = translator;
installTranslator(m_Translator); installTranslator(m_Translator);
} }
void QBarrierApplication::onLastWindowClosed()
{
// If there is no system tray available then quit when the last window is closed
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
quit();
}
}

View File

@ -37,6 +37,9 @@ class QBarrierApplication : public QApplication
static QBarrierApplication* getInstance(); static QBarrierApplication* getInstance();
private Q_SLOTS:
void onLastWindowClosed();
private: private:
QTranslator* m_Translator; QTranslator* m_Translator;

View File

@ -88,8 +88,6 @@ int main(int argc, char* argv[])
} }
#endif #endif
QApplication::setQuitOnLastWindowClosed(false);
QSettings settings; QSettings settings;
AppConfig appConfig (&settings); AppConfig appConfig (&settings);