I have a QML window embed inside a QWidget
via QWidget::createWindowContainer()
.
To provide some reloading capabilities without hiding the window, I would like to be able to replace the underlying (embed) qml window, without destroying and recreating the parent widget.
What I tried so far is
QQmlApplicationEngine engine;
// First time
// Load the qml file
engine.load("main.qml");
// Get the window
QWindow* window = static_cast<QWindow*>(engine.rootObjects().last());
// Create the window container
QWidget* container = QWidget::createWindowContainer(qmlWindow);
// -> This works perfectly well
// Other times
// Get the old window
QWindow* oldWindow = static_cast<QWindow*>(engine.rootObjects().last());
// And its container
QWindow* container = oldWindow->parent();
// Close the old window
lOldWindow->close();
// Load the qml
engine.load("main.qml");
// Get the new qml window
QWindow* window = static_cast<QWindow*>(engine.rootObjects().last());
// Reparent it to the old window's container
lWindow->setParent(lContainer);
// -> The newly created window does not show up
I used to completely delete the container window and recreate it as well (with createWindowContainer
) and this was working perfectly well, but I would like to avoid deleting the container widget over and over.
How can I achieve this ?
(Please note there is no pointer checking nor error handling for code simplicity, there is no need to comment about that :) )