From 5d5b5c14a9f432624b66a3dcf5789a5a8ed26372 Mon Sep 17 00:00:00 2001 From: 0AnshuAditya0 Date: Fri, 26 Dec 2025 13:36:17 +0530 Subject: [PATCH] Fix Qt HighGUI lifecycle issue when external QApplication exists Respect QApplication::quitOnLastWindowClosed() before calling qApp->quit(). This prevents OpenCV from unintentionally terminating externally managed Qt applications when the last HighGUI window is closed. Fixes #28291 --- modules/highgui/src/window_QT.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index 3746acb171..d327ad6603 100644 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -854,14 +854,17 @@ GuiReceiver::GuiReceiver() : bTimeOut(false), nb_windows(0) void GuiReceiver::isLastWindow() { - if (--nb_windows <= 0) + if (qApp->quitOnLastWindowClosed()) { - delete guiMainThread;//delete global_control_panel too - guiMainThread = NULL; - - if (doesExternalQAppExist) + if (--nb_windows <= 0) { - qApp->quit(); + delete guiMainThread; // delete global_control_panel too + guiMainThread = NULL; + + if (doesExternalQAppExist) + { + qApp->quit(); + } } } }