Embedding a QWindow inside a QML scene?
Is it possible to embed a QWindow inside a QQuickItem?
For example, if I have a QWindow that I'm using to render an OpenGL scene (or I just have a second QQuickView ) and I want to use it as an item embedded inside a QML scene.
I see that I can embed a QWindow inside a QWidget and I can embed a QWidget inside a QML scene, but can I embed a QWindow-based scene inside another QML scene?
I don't think so. Here are the alternatives:
- OpenGL scene: Use the "Qt Quick Scene Graph":http://qt-project.org/doc/qt-5/qtquick-visualcanvas-scenegraph.html
- Second QQuickView: Don't make a second QQuickView. Just load the QML file as a child component in your first QQuickView.
[quote author="dublindan" date="1402336417"]I can embed a QWidget inside a QML scene[/quote]This was possible in Qt Quick 1, but not Qt Quick 2.
I'm not actually trying to embed a QML scene within another QML scene, I am more interested how it could be done than actually doing it. What I really want to be able to do is embed any QWindow in a QML scene like you can do in QWidgets with QWidget::createWindowContainer.
What I want to do with this is embed external windows in a QML applications (for example, Java windows or SDL windows).
I've a hack that works well enough, but it would be nice to have proper integration. Basically, I can get the window id from the external window (both Swing and SDL2 let you do this easily if you have access to the source, I'm sure other GUI tools do too. Otherwise you can get it through platform API's), then I use QWindow::fromWinId(...) to create a new QWindow from the existing native window. Then I can setParent to reparent the window to my existing QML window and use mapToScene and position()/size() of the QML Item where I want to position the external window. When the "parent" item changes position or size, I need to change my QWindows position and size too. Now I have an external window "embedded" in a QML scene.
Of course, its far from perfect... for one, the window is basically just overlayed over the scene, so it doesn't play nice with QML elements that might be overlapping it. Even if you could embed properly, this may still be the case though. Also, I find that input capture is very... hit or miss... with this approach.
The second approach I've had success with (and input capture seems to work much better) is basically the opposite: create a QWindow, get its windId and then have the external window created from that id. SDL 2 supports this with SDL_CreateWindowFrom, not sure about other libraries though and this only works if you have access to the window creation code, so doesn't work for many use cases.
bq. This was possible in Qt Quick 1, but not Qt Quick 2.
Hmm. I was sure I saw something that was essentially the opposite of QWidget::createWindowContainer, but I can't find it now and don't have time to go digging... I know there was some work around Qt 5.0 or 5.1 to render QWidgets to a texture and then render that within QML and I thought I remembered 5.1.1 or 5.2 or something release it, but I could be wrong.