February 19, 2013 by Gunnar Sletta | Comments
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.
QQuickView *view = new QQuickView();
...
QWidget *container = QWidget::createWindowContainer(view);
container->setMinimumSize(...);
container->setMaximumSize(...);
container->setFocusPolicy(Qt::TabFocus);
widgetLayout->addWidget(container);
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.
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.
Enjoy!
Download the latest release here: www.qt.io/download.
Qt 6.8 release focuses on technology trends like spatial computing & XR, complex data visualization in 2D & 3D, and ARM-based development for desktop.
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Feb 26, 2025
We are happy to announce the release of the Qt Tools for Android Studio..
Jan 27, 2025
Qt Gradle Plugin 1.1 (QtGP) is here! If you missed 1.0 release, I..
Jan 23, 2025
We have released Qt AI Assistant to help you in cross-platform software..
Hello,
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.
Cheers.
Michal
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.
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?
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 https://codereview.qt-proje...
Thanks Gunnar, that's good good news!
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.
@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.
Thanks Tuukka.
Tuukka, thanks for the info. Besides the other QML widgets, do you plan to have a QML version of QDockWidget?
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. :)
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: https://bugreports.qt-proje...
Thanks Gunnar, will give that a try!
Gunnar: when you say QQuickWindow::setClearColor(Qt::transparent), I assume you are referring to QQuickWindow::setColor()?
Daniel: correct, setColor() is the right one. Sorry about the confusion
Like Andrew Jones, I also need binaries with this feature! Nice work!
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);
"
@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).
Great! You solved my problem.
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.
https://codereview.qt-proje...
Regards
Michael
Sorry link to the old review
here the right one
https://codereview.qt-proje...
Only wanna comment on few general things, The website design and style is perfect, the content material is really great : D.