Skip to content

Commit c038529

Browse files
authored
fix(linux): break QtMainLoop on exit if loop() was called blocking (#139)
1 parent a0381e7 commit c038529

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/QtTrayMenu.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void QtTrayMenu::onUpdate(struct tray *tray, const bool notify) {
112112
}
113113
}
114114

115-
int QtTrayMenu::loop(int blocking) const {
115+
int QtTrayMenu::loop(int blocking) {
116116
if (!running) {
117117
return -1;
118118
}
@@ -121,9 +121,11 @@ int QtTrayMenu::loop(int blocking) const {
121121
return -1;
122122
}
123123
if (blocking) {
124+
blockingEventLoop = true;
124125
QApplication::exec();
125126
return -1;
126127
} else {
128+
blockingEventLoop = false;
127129
QApplication::processEvents();
128130
return 0;
129131
}
@@ -149,6 +151,11 @@ void QtTrayMenu::onExitRequested() {
149151
}
150152
// Unset tray structure
151153
trayStruct = nullptr;
154+
155+
// If we run in a blocking event loop break said loop by quitting the QApplication
156+
if (blockingEventLoop) {
157+
QApplication::quit();
158+
}
152159
}
153160

154161
void QtTrayMenu::updateMenu(struct tray_menu *items) {

src/QtTrayMenu.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class QtTrayMenu: public QObject {
6060
* @param blocking if true the function call will block until QtTrayMenu exits
6161
* @return 0 on successful processing if non-blocking, -1 otherwise
6262
*/
63-
int loop(int blocking) const;
63+
int loop(int blocking);
6464

6565
/**
6666
* @brief Configure metadata for QApplication
@@ -135,6 +135,7 @@ class QtTrayMenu: public QObject {
135135
QMenu *trayTopMenu = nullptr;
136136
struct tray *trayStruct = nullptr;
137137
bool running = false;
138+
bool blockingEventLoop = false;
138139
struct tray_menu *getTrayMenuItem(QAction *action);
139140
std::function<void()> notificationCallback = nullptr;
140141

0 commit comments

Comments
 (0)