Introducing QWidget::createWindowContainer()

Published Tuesday February 19th, 2013 | by

Qt 5 introduced a new set of OpenGL classes in Qt Gui and a new rendering pipeline for Qt Quick with the scenegraph. As awesome as these are, they were based on the newly introduced QWindow, making it very hard to use them in existing applications.

To remedy this problem, Qt 5.1 introduces the function QWidget::createWindowContainer(). A function that creates a QWidget wrapper for an existing QWindow, allowing it to live inside a QWidget-based application. Using QQuickView or QOpenGLContext together with widgets is now possible.

How to use

QQuickView *view = new QQuickView();

QWidget *container = QWidget::createWindowContainer(view);


How it works

The window container works by forcing the use of native child widgets inside the widgets hierarchy and will reparent the window in the windowing system. After that, the container will manage the window’s geometry and visibility. The rendering of the window happens directly in the window without any interference from the widgets, resulting in optimal performance.

As can be seen from the code-snippet above, the container can also receive focus.

Embedding the “Other Way”

This feature covers the use case where an application wants to either port an existing view to either Qt Quick or the new OpenGL classes in Qt Gui. What about the use case where an application’s mainview is written with widgets, say QGraphicsView, and the application wants to keep this and rewrite the surrounding UI with Qt Quick? Well, this is doable by keeping the main application QWidget-based and making each of the big UI blocks a QQuickView embedded in its own container.


Did you like this? Share it:
Bookmark and Share

Posted in OpenGL, Qt, Qt Quick 2.0

22 comments to Introducing QWidget::createWindowContainer()

michal says:


Could you please explain “The Other Way” a little bit more?
It would be great if you added a code snippet explaining how to embed a QWidget into a QtQuick application.


Gunnar Sletta says:

michal: What I’m saying in the last paragraph is that you don’t need to embed a QWidget into a QQuick application. You keep the entire application as a QWidget based one and replace for instance, the toolbar and the right-hand side dock with a QQuickView.

Benoit says:

But is it actually possible to embed a QWidget into a QQuick application? And to go even further, will it be possible to embed views/items from external processes using something like the XEmbed protocol?

Gunnar Sletta says:

Benoit: It is not possible to embed a QWidget into a QtQuick application. We have so far concluded that the window container for QWidgets is enough, based on that it allows you to embed QML into a widgets app. In the case where you have a main-view which is a widget and you want a Qt Quick UI around it, you can achieve that by creating several window containers. Another alternative, if your main-widget is a “scene renderer” type widget which does most of its work in the paintEvent, is to change your widget to be a QQuickPaintedItem and use the paint() function instead.

XEmbed is being worked on for QWindow/QWidget,42990

Benoit says:

Thanks Gunnar, that’s good good news!

Andrew Jones says:

I think this is exactly what I need to solve this problem.

Now the big question :) when is 5.1 expected to come out? I tried to build it from source so I could use this new feature but can’t get past this issue.

Tuukka Turunen says:

@Andrew Jones: We are working towards getting 5.1.0 out in the beginning of May with beta, RC and the works coming before that. We are continuously blogging on how the different areas are ongoing so you can follow the release content shape up.

Tuukka, thanks for the info. Besides the other QML widgets, do you plan to have a QML version of QDockWidget?

Jens says:

We don’t yet have plans for that as we will need to focus on getting the more fundamental controls in first, but as this blog points out, you will now be able to easily work around that limitation as you can insert QtQuickViews into the existing QDockWidget framework.

Jens, thank you for your reply. I appreciate your advice. However, I’m looking forward to the day I’ll be able to build any type of GUI application with just QML. :)

Gunnar Sletta says:

Andrew Jones: This feature solves a different problem than the one you are referring to. In your case you want to:
– request an OpenGL surface with alpha on it, QSurfaceFormat::setAlphaBufferSize()
– QQuickWindow::setClearColor(Qt::transparent), the default is opaque white. (Edit: That should be QQuickWindow::setColor(Qt::transparent))
– QWindow::setFlags(Qt::FramelessWindowHint);
And then it relies on this being implemented on the platform. Currently this doesn’t always work:

Andrew Jones says:

Thanks Gunnar, will give that a try!

Daniel says:

Gunnar: when you say QQuickWindow::setClearColor(Qt::transparent), I assume you are referring to QQuickWindow::setColor()?

Gunnar Sletta says:

Daniel: correct, setColor() is the right one. Sorry about the confusion

João Barbosa says:

Like Andrew Jones, I also need binaries with this feature! Nice work!

Qt Quick Designer says:

Can I design the QQuickView object in Qt Quick Designer without coding? I think the following code should be generated by designer automatically:

QQuickView *view = new QQuickView();

QWidget *container = QWidget::createWindowContainer(view);

Kai Koehne says:

@Qt Quick Designer: I don’t think this belongs into the Qt Quick Designer, since there’s no way to automatically guess where to add that code. But if there’s demand we could provide a wizard for this kind of “mixed” applications inside Qt Creator. Feel free to create a suggestion, preferably with an example how the minimal hello world should look like.

(If you want to add this to existing code we could also a ‘create window container’ refactoring operation. Anyhow, given that you typically won’t do this kind of wrapping multiple times in a project I’m not sure a refactoring operation does really gain you that much).

wspilot says:

Great! You solved my problem.

mbreier says:

If you don’t want to wait until Qt 5.1 and you are willing to built your Qt Framework from Git, you can download this new feature from the Qt code review pages.,41539


mbreier says:

Sorry link to the old review
here the right one,44285

Only wanna comment on few general things, The website design and style is perfect, the content material is really great : D.

Commenting closed.