In Qt4, we could use the embedInto
method of the QX11EmbedWidget
class to embed any QWidget
into another application. But in Qt5, the QX11EmbedWidget
class was dropped. I've been searching Google for many hours, but all I found indicated to use the QWidget::createWindowContainer
method instead. However, as far as I understand, this method is rather a replacement for QX11EmbedContainer
.
So here's the question: How do we embed a Qt5-Widget into a non-Qt window? - And in case it metters: The non-Qt window is Gtk3.
I've been asked for the use-case, so let me illustrate briefly: Consider a Gtk-based application, which you are willing to extend - and the UI component, which you need, already exists, but is written in Qt. The goal is to avoid porting the application to Qt or the component to Gtk.
My initial approach was to request the embedding from Qt side. I had no luck with this, as I couldn't find any equivalent for
QX11EmbedWidget::embedInto
in Qt5. After giving up on this, I decided to giveXReparentWindow
a try, which was reportedly used with success. However, due to lack of documentation, I wasn't able to reproduce this. My third attempt was to initiate the embedding from server side, which is the Gtk window in my case. I'm glad to report, that it worked :-)For the Record: How to embed an arbitrary Qt5 Widget into a Gtk window
TL;DR:
gtk_socket_add_id
to embed any X11 window into aGtkSocket
.Also see the documentation for reference, but note that the example given their doesn't even compile, because they forgot the
GTK_SOCKET
macro. On the contrary, the following code works.Details
The
QGtkWindow
class provides an interface to the Gtk window.The
GtkWindowAdapter
class wraps the Gtk calls and isolates them from the Qt part of the application. An object of this class represents the Gtk window.When instantiated, an
GtkWindowAdapter
object first initializes Gtk,and then setups the Gtk window:
You might already have noted the
embed
method which this class provides. This method initiates the embedding of any X11 window. Theshow
method turns the encapsulated Gtk window visible.Now, the implementation of the
QGtkWindow
class is fairly simple. When created, it initializes a Gtk window by using theGtkWindowApdater
and puts an emptyQWidget
into that window:When the user of the
QGtkWindow
class decides to put some widget into the window, thesetCentralWidget
is the way to go. It simply clears the parent widget, which was embedded into the Gtk window originally, then inserts the user's widget instead:Hope this might spare somebody many hours.