From 828735f1a3b39cad3bef3a7465a37bb33fa47b7c Mon Sep 17 00:00:00 2001 From: tytan652 Date: Thu, 11 Apr 2024 16:28:40 +0200 Subject: [PATCH] UI,docs: Send a custom event to the dock widget when closing --- UI/window-basic-main.cpp | 2 ++ UI/window-dock.cpp | 5 +++++ docs/sphinx/reference-frontend-api.rst | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index 69f09bcec28657..ac1dd63803a255 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -329,6 +329,8 @@ OBSBasic::OBSBasic(QWidget *parent) setContextMenuPolicy(Qt::CustomContextMenu); + QEvent::registerEventType(QEvent::User + QEvent::Close); + api = InitializeAPIInterface(this); ui->setupUi(this); diff --git a/UI/window-dock.cpp b/UI/window-dock.cpp index eadd3724acebfe..a057add5b17ced 100644 --- a/UI/window-dock.cpp +++ b/UI/window-dock.cpp @@ -34,6 +34,11 @@ void OBSDock::closeEvent(QCloseEvent *event) } QDockWidget::closeEvent(event); + + if (widget() && event->isAccepted()) { + QEvent widgetEvent(QEvent::Type(QEvent::User + QEvent::Close)); + qApp->sendEvent(widget(), &widgetEvent); + } } void OBSDock::showEvent(QShowEvent *event) diff --git a/docs/sphinx/reference-frontend-api.rst b/docs/sphinx/reference-frontend-api.rst index 0a88451c2c95da..5e95b4c09302f0 100644 --- a/docs/sphinx/reference-frontend-api.rst +++ b/docs/sphinx/reference-frontend-api.rst @@ -480,6 +480,12 @@ Functions Adds a dock with the widget to the UI with a toggle in the Docks menu. + When the dock is closed, a custom QEvent of type `QEvent::User + QEvent::Close` + is sent to the widget to enable it to react to the event (e.g., unload elements + to save resources). + A generic QShowEvent is already sent by default when the widget is being + shown (e.g., dock opened). + Note: Use :c:func:`obs_frontend_remove_dock` to remove the dock and the id from the UI.