Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void reload(QQmlEngine &engine, const QUrl &qmlUrl)
- {
- engine.clearComponentCache();
- // Maps to store old window properties
- static QMap<QString, QRect> oldGeometries;
- static QMap<QString, QScreen *> oldScreens;
- static QMap<QString, bool> isVisible;
- // Collect information from existing windows
- auto rootObjects = engine.rootObjects();
- for (QObject *obj : rootObjects)
- {
- QWindow *window = qobject_cast<QWindow *>(obj);
- if (window && !window->property("scheduledForDeletion").toBool())
- {
- QString objectName = window->objectName();
- oldGeometries[objectName] = window->geometry();
- oldScreens[objectName] = window->screen();
- isVisible[objectName] = window->isVisible();
- window->setProperty("scheduledForDeletion", true);
- window->deleteLater();
- }
- }
- // Load new QML file from the provided URL
- engine.load(qmlUrl);
- // Apply old properties to the new windows based on objectName
- for (QObject *obj : engine.rootObjects())
- {
- QWindow *newWindow = qobject_cast<QWindow *>(obj);
- if (newWindow)
- {
- QString objectName = newWindow->objectName();
- if (oldGeometries.contains(objectName))
- {
- if (oldScreens.contains(objectName))
- {
- newWindow->setScreen(oldScreens[objectName]); // Set screen before geometry
- }
- newWindow->setGeometry(oldGeometries[objectName]);
- if (isVisible[objectName])
- {
- qDebug() << "Showing window with objectName:" << objectName;
- newWindow->show();
- newWindow->raise();
- // newWindow->requestActivate(); // Bring window to foreground and request focus
- }
- else
- {
- newWindow->hide();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment