Guest User

Untitled

a guest
Sep 7th, 2024
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. void reload(QQmlEngine &engine, const QUrl &qmlUrl)
  2. {
  3.     engine.clearComponentCache();
  4.     // Maps to store old window properties
  5.     static QMap<QString, QRect> oldGeometries;
  6.     static QMap<QString, QScreen *> oldScreens;
  7.     static QMap<QString, bool> isVisible;
  8.  
  9.     // Collect information from existing windows
  10.     auto rootObjects = engine.rootObjects();
  11.     for (QObject *obj : rootObjects)
  12.     {
  13.         QWindow *window = qobject_cast<QWindow *>(obj);
  14.         if (window && !window->property("scheduledForDeletion").toBool())
  15.         {
  16.             QString objectName = window->objectName();
  17.             oldGeometries[objectName] = window->geometry();
  18.             oldScreens[objectName] = window->screen();
  19.             isVisible[objectName] = window->isVisible();
  20.             window->setProperty("scheduledForDeletion", true);
  21.             window->deleteLater();
  22.         }
  23.     }
  24.  
  25.     // Load new QML file from the provided URL
  26.     engine.load(qmlUrl);
  27.  
  28.     // Apply old properties to the new windows based on objectName
  29.     for (QObject *obj : engine.rootObjects())
  30.     {
  31.         QWindow *newWindow = qobject_cast<QWindow *>(obj);
  32.         if (newWindow)
  33.         {
  34.             QString objectName = newWindow->objectName();
  35.             if (oldGeometries.contains(objectName))
  36.             {
  37.                 if (oldScreens.contains(objectName))
  38.                 {
  39.                     newWindow->setScreen(oldScreens[objectName]); // Set screen before geometry
  40.                 }
  41.                 newWindow->setGeometry(oldGeometries[objectName]);
  42.  
  43.                 if (isVisible[objectName])
  44.                 {
  45.                     qDebug() << "Showing window with objectName:" << objectName;
  46.                     newWindow->show();
  47.                     newWindow->raise();
  48.                     // newWindow->requestActivate(); // Bring window to foreground and request focus
  49.                 }
  50.                 else
  51.                 {
  52.                     newWindow->hide();
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment