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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2023-04-28 19:59:52 +02:00
parent 15bf145f14
commit f831027d65
2 changed files with 20 additions and 0 deletions

View File

@ -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=<nr> 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);

View File

@ -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<QApplication> app(new QApplication(argc, argv));
QStringList files;
QStringList importedFiles;