diff --git a/UI/qt-display.cpp b/UI/qt-display.cpp index 4b24b4581..3da4c7bc7 100644 --- a/UI/qt-display.cpp +++ b/UI/qt-display.cpp @@ -13,6 +13,39 @@ #include #endif +class SurfaceEventFilter : public QObject { + OBSQTDisplay *display; + +public: + SurfaceEventFilter(OBSQTDisplay *src) : display(src) {} + +protected: + bool eventFilter(QObject *obj, QEvent *event) override + { + bool result = QObject::eventFilter(obj, event); + QPlatformSurfaceEvent *surfaceEvent; + + switch (event->type()) { + case QEvent::PlatformSurface: + surfaceEvent = + static_cast(event); + + switch (surfaceEvent->surfaceEventType()) { + case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed: + display->DestroyDisplay(); + break; + default: + break; + } + break; + default: + break; + } + + return result; + } +}; + static inline long long color_to_int(const QColor &color) { auto shift = [&](unsigned val, int shift) { @@ -65,6 +98,8 @@ OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags) connect(windowHandle(), &QWindow::visibleChanged, windowVisible); connect(windowHandle(), &QWindow::screenChanged, screenChanged); + + windowHandle()->installEventFilter(new SurfaceEventFilter(this)); } QColor OBSQTDisplay::GetDisplayBackgroundColor() const @@ -92,6 +127,9 @@ void OBSQTDisplay::CreateDisplay() if (display) return; + if (destroying) + return; + if (!windowHandle()->isExposed()) return; diff --git a/UI/qt-display.hpp b/UI/qt-display.hpp index 44946da3a..7a90f10fb 100644 --- a/UI/qt-display.hpp +++ b/UI/qt-display.hpp @@ -12,6 +12,7 @@ class OBSQTDisplay : public QWidget { SetDisplayBackgroundColor) OBSDisplay display; + bool destroying = false; virtual void paintEvent(QPaintEvent *event) override; virtual void moveEvent(QMoveEvent *event) override; @@ -38,7 +39,11 @@ public: void SetDisplayBackgroundColor(const QColor &color); void UpdateDisplayBackgroundColor(); void CreateDisplay(); - void DestroyDisplay() { display = nullptr; }; + void DestroyDisplay() + { + display = nullptr; + destroying = true; + }; void OnMove(); void OnDisplayChange(); diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index 9b4334314..02b1dafed 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -5154,10 +5154,6 @@ void OBSBasic::closeEvent(QCloseEvent *event) delete extraBrowsers; - ui->preview->DestroyDisplay(); - if (program) - program->DestroyDisplay(); - config_set_string(App()->GlobalConfig(), "BasicWindow", "DockState", saveState().toBase64().constData());