Embed a GTK Widget into a Qt Window

2.5k Views Asked by At

I need to develop an application for Windows, Linux and Mac. To not have to write all the window cruft myself I chose to use Qt5 (over wxWidgets, because the latter ship no precompiled binaries). I have a GTK Widget (of Cef) which I now need to embed, but sadly have no idea how.

There seems to have been QX11EmbedContainer in previous versions of Qt, but it is not present anymore and also I am not sure this works, when there is a switch to Weyland.

3

There are 3 best solutions below

2
On BEST ANSWER

You can try the QT WebKit if you need just a embed browser

http://qt-project.org/doc/qt-5.0/qtwebkit/qtwebkit-index.html

1
On
    QMainWindow* main_window = new QMainWindow;
    QX11EmbedContainer* container = new QX11EmbedContainer;
    main_window->setCentralWidget(container);

    //gtk code
    GtkWidget* window;
    GtkWidget* button;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new ();
    gtk_widget_show (button);
    gtk_container_add (GTK_CONTAINER (window), button);
    gtk_widget_show(window);
    XID id = GDK_WINDOW_XWINDOW (GTK_WIDGET(window)->window);

    container->embedClient(id);

you can use QX11EmbedContainer class on Qt4.

0
On

It's been 7 years, but I've dealt with this before. Here's what I found1:

I found a way to properly embed CEF inside a Qt window: embedding it inside an empty QWindow instead of a QWidget. There are some caveats, though:

  1. To add the CEF window into a QWidget layout, you will need to use QWidget::createWindowContainer()

  2. An empty QWindow doesn't render anything -- not even a background. So, you might need to use a QBackingStore to make it render something when CEF isn't embedded -- see the Raster Window example for some reference.

  3. You might need to use the winId from before you add the QWindow into your widget's layout.

I highly recommend anyone still trying this to take a look at the entire thread1. QtWebkit is not longer an acceptable solution, while CEF centainly is.