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::embedIntoin Qt5. After giving up on this, I decided to giveXReparentWindowa 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_idto 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_SOCKETmacro. On the contrary, the following code works.Details
The
QGtkWindowclass provides an interface to the Gtk window.The
GtkWindowAdapterclass 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
GtkWindowAdapterobject first initializes Gtk,and then setups the Gtk window:
You might already have noted the
embedmethod which this class provides. This method initiates the embedding of any X11 window. Theshowmethod turns the encapsulated Gtk window visible.Now, the implementation of the
QGtkWindowclass is fairly simple. When created, it initializes a Gtk window by using theGtkWindowApdaterand puts an emptyQWidgetinto that window:When the user of the
QGtkWindowclass decides to put some widget into the window, thesetCentralWidgetis 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.