QGtk3Theme: Ensure gtk uses the same windowing system as Qt

Fixes: QTBUG-69354
Pick-to: 6.4 6.3 6.2 5.15
Change-Id: I254c5bf98bc3b2cc2577159e475a2105438bb96b
Reviewed-by: Liang Qi <liang.qi@qt.io>
This commit is contained in:
Ilya Fedin 2022-07-02 13:56:00 +04:00
parent f1a3069afb
commit 3d45f23b02

View File

@ -6,6 +6,7 @@
#include "qgtk3menu.h" #include "qgtk3menu.h"
#include <QVariant> #include <QVariant>
#include <QtCore/qregularexpression.h> #include <QtCore/qregularexpression.h>
#include <QGuiApplication>
#undef signals #undef signals
#include <gtk/gtk.h> #include <gtk/gtk.h>
@ -14,6 +15,8 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
const char *QGtk3Theme::name = "gtk3"; const char *QGtk3Theme::name = "gtk3";
template <typename T> template <typename T>
@ -49,6 +52,14 @@ void gtkMessageHandler(const gchar *log_domain,
QGtk3Theme::QGtk3Theme() QGtk3Theme::QGtk3Theme()
{ {
// Ensure gtk uses the same windowing system, but let it
// fallback in case GDK_BACKEND environment variable
// filters the preferred one out
if (QGuiApplication::platformName().startsWith("wayland"_L1))
gdk_set_allowed_backends("wayland,x11");
else if (QGuiApplication::platformName() == "xcb"_L1)
gdk_set_allowed_backends("x11,wayland");
// gtk_init will reset the Xlib error handler, and that causes // gtk_init will reset the Xlib error handler, and that causes
// Qt applications to quit on X errors. Therefore, we need to manually restore it. // Qt applications to quit on X errors. Therefore, we need to manually restore it.
int (*oldErrorHandler)(Display *, XErrorEvent *) = XSetErrorHandler(nullptr); int (*oldErrorHandler)(Display *, XErrorEvent *) = XSetErrorHandler(nullptr);