From f831027d6598471669dc949035942ac2d532a7b7 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 28 Apr 2023 19:59:52 +0200 Subject: [PATCH] desktop: set Qt::AA_ShareOpenGLContexts application flag Qt6 destroys QQuickItems when reparenting their parent widget. This can be prevented for OpenGL-backends by setting this application-wide flag on startup. Sadly, it doesn't help for non-OpenGL based applications. Make the sharing optional (by commandline flag --no-opengl-sharing) to test the destruction/recreation of QQuickItems case. Signed-off-by: Berthold Stoeger --- core/subsurfacestartup.c | 5 +++++ subsurface-desktop-main.cpp | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/core/subsurfacestartup.c b/core/subsurfacestartup.c index 9e66ce625..abc63ef6c 100644 --- a/core/subsurfacestartup.c +++ b/core/subsurfacestartup.c @@ -91,6 +91,7 @@ static void print_help() printf("\n --dc-product=product Set the dive computer to download from"); printf("\n --device=device Set the device to download from"); #endif + printf("\n --no-opengl-sharing Don't share OpenGL contexts between QQuickItems (for testing)"); printf("\n --cloud-timeout= Set timeout for cloud connection (0 < timeout < 60)\n\n"); } @@ -149,6 +150,10 @@ void parse_argument(const char *arg) ++force_root; return; } + if (strcmp(arg, "--no-opengl-sharing") == 0) { + // This was already checked in main() + return; + } #if SUBSURFACE_DOWNLOADER if (strncmp(arg, "--dc-vendor=", sizeof("--dc-vendor=") - 1) == 0) { prefs.dive_computer.vendor = strdup(arg + sizeof("--dc-vendor=") - 1); diff --git a/subsurface-desktop-main.cpp b/subsurface-desktop-main.cpp index 918974f13..6610f5e8f 100644 --- a/subsurface-desktop-main.cpp +++ b/subsurface-desktop-main.cpp @@ -13,6 +13,7 @@ #include "core/qt-gui.h" #include "core/qthelper.h" #include "core/subsurfacestartup.h" +#include "core/subsurface-string.h" #include "core/settings/qPref.h" #include "core/tag.h" #include "desktop-widgets/mainwindow.h" @@ -41,6 +42,20 @@ int main(int argc, char **argv) int i; bool no_filenames = true; QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); + + // Allow OpenGL-based QtQuick backends to share OpenGL contexts. + // This avoids destruction of QQuickItems if their parent widgets are moved between widgets in Qt6. + // Sadly, this does not help when running on non-OpenGL backends. + // Allow for turning this off to test destruction of QQuickItems. + // The parameter is checked here because QCoreApplication::parameters() only + // works after instantiation of the QApplication class, but the flag has + // to be set bofore. + if (std::any_of(argv + 1, argv + argc, + [](char *arg) { return same_string(arg, "--no-opengl-sharing"); })) { + fprintf(stderr, "Disabling sharing of OpenGL contexts\n"); + } else { + QGuiApplication::setAttribute(Qt::AA_ShareOpenGLContexts); + } std::unique_ptr app(new QApplication(argc, argv)); QStringList files; QStringList importedFiles;