From 6b4ef1dccd8b2d5f1727081f3eb53a1c4493a44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Walle?= Date: Fri, 28 Mar 2025 14:27:17 +0100 Subject: [PATCH] Fix closing of windows when using the Qt backend Notify the main GUI thread upon receiving a close event instead of when the windows is destroyed. Additionally there was a logic error in in GuiReceiver::isLastWindow() that is corrected. Fixes #6479 and #20822 --- modules/highgui/src/window_QT.cpp | 18 ++++++++++-------- modules/highgui/src/window_QT.h | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index c8957d9536..f7cd08bc88 100644 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -859,7 +859,7 @@ void GuiReceiver::isLastWindow() delete guiMainThread;//delete global_control_panel too guiMainThread = NULL; - if (!doesExternalQAppExist) + if (doesExternalQAppExist) { qApp->quit(); } @@ -1739,13 +1739,6 @@ CvWindow::CvWindow(QString name, int arg2) } -CvWindow::~CvWindow() -{ - if (guiMainThread) - guiMainThread->isLastWindow(); -} - - void CvWindow::setMouseCallBack(CvMouseCallback callback, void* param) { myView->setMouseCallBack(callback, param); @@ -2259,6 +2252,15 @@ void CvWindow::keyPressEvent(QKeyEvent *evnt) } +void CvWindow::closeEvent(QCloseEvent* evnt) +{ + QWidget::closeEvent(evnt); + + if (guiMainThread) + guiMainThread->isLastWindow(); +} + + void CvWindow::icvLoadControlPanel() { QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName() + " control panel"); diff --git a/modules/highgui/src/window_QT.h b/modules/highgui/src/window_QT.h index b93b9ba597..1224ba9d32 100644 --- a/modules/highgui/src/window_QT.h +++ b/modules/highgui/src/window_QT.h @@ -298,7 +298,6 @@ class CvWindow : public CvWinModel Q_OBJECT public: CvWindow(QString arg2, int flag = CV_WINDOW_NORMAL); - ~CvWindow(); void setMouseCallBack(CvMouseCallback m, void* param); @@ -349,6 +348,7 @@ public: protected: virtual void keyPressEvent(QKeyEvent* event) CV_OVERRIDE; + virtual void closeEvent(QCloseEvent* event) CV_OVERRIDE; private: