Ya sure , I will put my latest code here.
I have designed this below code like if the user launches the application for the first time , then the application will load Mainwindow.qml or basic.qml based on the flag "bSettingMainWindow" .
Next time based on the switch button click from application will load another window (other than first one) and making the first launched qml window visible false.
Once both of them launched once, then 2 windows will play with visibility true or false on click of switch button or signal is emitted.
This below code is working perfectly fine except the window is not coming on top after making visible true. I have tried using raise() and activatewindow() even after that i am facing the same issue.
Please find the code below


Qt Code:
  1. //CustomWindow.h
  2. class CustomWindow : public QQuickWindow {
  3. //Custom QQuick window used in QML
  4. }
  5.  
  6.  
  7.  
  8. //** windowLoader.hpp **//
  9. class WindowLoader : public QObject{
  10. Q_OBJECT
  11. QQmlApplicationEngine loadQMlEngine;
  12.  
  13. public:
  14. explicit WindowLoader(QObject * parent = 0);
  15. void loadWindow();
  16.  
  17.  
  18. public slots:
  19. void loadMainWindow();
  20. void loadBasicWindow();
  21. void connectToMain(QObject *object = nullptr, const QUrl &url = QUrl(""));
  22. void connectToBasic(QObject *object = nullptr, const QUrl &url = QUrl(""));
  23.  
  24. private:
  25. };
  26.  
  27.  
  28.  
  29. //** windowLoader.cpp **//
  30. WindowLoader::WindowLoader(QObject *parent) : QObject(parent) {
  31.  
  32. }
  33.  
  34. //This function will be called only one time during launch
  35. void WindowLoader::loadWindow() {
  36. if(bSettingMainWindow){ //this will be from a internal flag, this check is only one time during launch
  37. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)),Qt::QueuedConnection);
  38. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  39. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
  40. m_iMainWindowIndex = 0;
  41. m_iBasicWindowIndex = 1;
  42. } else {
  43. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)),Qt::QueuedConnection);
  44. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  45. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
  46. m_iMainWindowIndex = 1;
  47. m_iBasicWindowIndex = 0;
  48. }
  49. }
  50.  
  51. void WindowLoader::connectToBasic(QObject *object, const QUrl &url) {
  52. if(object){
  53. connect(object, SIGNAL(switchToBasicSignal()), this, SLOT(loadBasicWindow()));
  54. }
  55. }
  56.  
  57. void WindowLoader::connectToMain(QObject *object, const QUrl &url) {
  58. if(object){
  59. connect(object, SIGNAL(switchToMainSignal()), this, SLOT(loadMainWindow()));
  60. }
  61. }
  62.  
  63. void WindowLoader::loadBasicWindow() {
  64.  
  65. if(loadQMlEngine.rootObjects().size() <= 2) {
  66. disconnect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)));
  67. }
  68.  
  69. if(loadQMlEngine.rootObjects().size() < 2) {
  70. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)),Qt::QueuedConnection);
  71. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  72. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
  73. loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
  74. } else {
  75. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"show");
  76. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"hide");
  77. loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",true);
  78. loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
  79. }
  80. }
  81.  
  82. void WindowLoader::loadMainWindow() {
  83.  
  84. if(loadQMlEngine.rootObjects().size() <= 2) {
  85. disconnect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)));
  86. }
  87.  
  88. if(loadQMlEngine.rootObjects().size() < 2) {
  89. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)),Qt::QueuedConnection);
  90. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
  91. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
  92. loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
  93. } else {
  94. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"hide");
  95. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"show");
  96. loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
  97. loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",true);
  98. }
  99. }
  100.  
  101.  
  102. //** main.cpp **//
  103. int main( int argc, char *argv[] ) {
  104. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  105. QApplication app(argc, argv);
  106. qmlRegisterType<CustomWindow>("CNQml", 1, 0, "CustomWindow");
  107. WindowLoader root;
  108. root.loadWindow();
  109. return app.exec();
  110. }
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. // ** mainWindow.qml **//
  2. CustomWindow {
  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. CustomWindow {
  23. visible: true
  24. width: 640
  25. height: 480
  26. title: qsTr("basicwindow")
  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