0

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 :) )

cmourglia
  • 2,423
  • 1
  • 17
  • 33
  • Could you just have two widgets and then set their visibility? That's the approach I usually take. – Clearer Jan 03 '18 at 10:18
  • I'm not sure to understand what you mean, could you clarify please ? – cmourglia Jan 03 '18 at 10:21
  • You say you want to replace the underlying qml window -- this implies removing it from the parent and adding a new window to the parent. I'm suggesting that you simply keep both windows in the parent at the same time, and simply showing the one you want and hiding the one you don't need. – Clearer Jan 03 '18 at 10:31
  • This cannot work. The problem is that what is returned by `QWidget::createWindowContainer` is in fact not a QWindow but a QWindowContainer, which is a private class – cmourglia Jan 03 '18 at 11:53

0 Answers0