Hello,
I have requirement to launch 2 different Qml files "mainwindow.qml" and "basic.qml" ,wheras both are independent of each other.
Initially i need to launch qml window based on flag bSettingMainWindow, based on this i will be launching any one of the qml file.
After launching I need to switch between these 2 qml files anytime. It should be like loading and unloading of "mainwindow.qml" and "basic.qml", based on the user action.
Since because memory concern , i need load any of them at a time. and i dont want to play with visible true or false.
Below is the code, I have written for to do this operation.
whereas from this below code I can able to load any one of the qml file based on the flag bSettingMainWindow. and also after loading
i can able to switch to another qml file window.
Assume first I have launched in Mainwindow.qml then i clicked the mouse button over Rectangle of mainwindow.qml and moved to basic.qml and now if i click on the Rectangle of basic.qml it is not
switching to mainwindow.qml.
and vice versa.
Only one time i can able to switch b/w these 2 . I want to switch multiple times.
Below is the code , requesting to please provide your answers

Qt Code:
  1. //** windowLoader.hpp **//
  2. class WindowLoader : public QObject{
  3. Q_OBJECT
  4. QQmlApplicationEngine loadQMlEngine;
  5.  
  6. public:
  7. explicit WindowLoader(QObject * parent = 0);
  8. void loadWindow();
  9.  
  10.  
  11. public slots:
  12. void loadMainWindow();
  13. void loadBasicWindow();
  14. void connectToMain(QObject *object = nullptr, const QUrl &url = QUrl(""));
  15. void connectToBasic(QObject *object = nullptr, const QUrl &url = QUrl(""));
  16.  
  17. private:
  18. };
  19.  
  20. //** windowLoader.cpp **//
  21. WindowLoader::WindowLoader(QObject *parent) : QObject(parent) {
  22.  
  23. }
  24.  
  25. void WindowLoader::loadWindow() {
  26. if(bSettingMainWindow){ //this will be from a internal flag, this check is only one time during launch
  27. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)),Qt::QueuedConnection);
  28. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  29. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
  30. } else {
  31. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)),Qt::QueuedConnection);
  32. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  33. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
  34. }
  35. }
  36.  
  37. void WindowLoader::connectToBasic(QObject *object, const QUrl &url) {
  38. if(object){
  39. connect(object, SIGNAL(switchToBasicSignal()), this, SLOT(loadBasicWindow()));
  40. }
  41. }
  42.  
  43. void WindowLoader::connectToMain(QObject *object, const QUrl &url) {
  44. if(object){
  45. connect(object, SIGNAL(switchToMainSignal()), this, SLOT(loadMainWindow()));
  46. }
  47. }
  48.  
  49. void WindowLoader::loadBasicWindow() {
  50. loadQMlEngine.rootObjects()[0]->deleteLater();
  51. loadQMlEngine.destroyed();
  52.  
  53. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  54. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
  55. }
  56.  
  57. void WindowLoader::loadMainWindow() {
  58. loadQMlEngine.rootObjects()[0]->deleteLater();
  59. loadQMlEngine.destroyed();
  60.  
  61. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  62. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
  63.  
  64. }
  65.  
  66.  
  67. //** main.cpp **//
  68. int main( int argc, char *argv[] ) {
  69. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  70. QApplication app(argc, argv);
  71. WindowLoader root;
  72. root.loadWindow();
  73. return app.exec();
  74. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. // ** mainWindow.qml **//
  2. ApplicationWindow {
  3. visible: true
  4. width: 1200
  5. height: 800
  6. title: qsTr("MainWindow")
  7.  
  8. signal switchToBasicSignal()
  9.  
  10. Rectangle {
  11. anchors.fill: parent
  12. MouseArea{
  13. anchors.fill: parent
  14. onClicked: {
  15. switchToBasicSignal()
  16. }
  17. }
  18. }
  19. }
  20.  
  21. //** basic.qml **//
  22. ApplicationWindow {
  23. visible: true
  24. width: 640
  25. height: 480
  26. title: qsTr("basic")
  27.  
  28. signal switchToMainSignal()
  29.  
  30. Rectangle {
  31. anchors.fill: parent
  32. MouseArea{
  33. anchors.fill: parent
  34. onClicked: {
  35. switchToMainSignal()
  36. }
  37. }
  38. }
  39. }
To copy to clipboard, switch view to plain text mode