Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Start an QML from within an IF statement
Forum Updated to NodeBB v4.3 + New Features

Start an QML from within an IF statement

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Collateral Studios
    wrote on last edited by
    #1

    Maybe the question seems trivial to many of you, but i would like to know how to start a QML only if a given condition occurs. Currently, my QML is loaded, but only if I comment on the IF statment

    # code block
    //if ( loadSettings("live_on_monitor_1", "1").toInt() != 0 ) {
            master_id = loadSettings("master_id", "1");
            start_url = getSportURI(loadSettings("sport_on_monitor_1", "1").toInt(), m_id);
    
            QRect primaryScreen = QGuiApplication::screens()[0]->availableGeometry();
            QQmlApplicationEngine engineLive_1;
    
            qInfo() << "Monitor Enabled:" << 1;
            qInfo() << "Monitor StartX:" << primaryScreen.x();
            qInfo() << "Monitor StartURL:" << start_url;
    
            engineLive_1.rootContext()->setContextProperty(QStringLiteral("startX"), primaryScreen.x());
            engineLive_1.rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress());
            engineLive_1.rootContext()->setContextProperty(QStringLiteral("start_url"), start_url);
    
            engineLive_1.load(QUrl(QLatin1String("qrc:/main.qml")));
        //}
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Collateral Studios
      wrote on last edited by
      #2

      I forgot to add .... in debug... when execute the load statement ...

      engineLive_1.load(QUrl(QLatin1String("qrc:/main.qml")));
      

      i get stuck again in

      QQmlApplicationEngine engineLive_1;
      

      and then proceeds to execute the next code.

      Also if i add the code in a function, and i try to call the function, i get the same result

      bool openWindow(int numScreen, QString start_url){
          QRect primaryScreen = QGuiApplication::screens()[numScreen-1]->availableGeometry();
          QQmlApplicationEngine engineLive;
      
          qInfo() << "Monitor Enabled:" << numScreen;
          qInfo() << "Monitor StartX:" << primaryScreen.x();
          qInfo() << "Monitor StartURL:" << start_url;
      
          engineLive.rootContext()->setContextProperty(QStringLiteral("startX"), primaryScreen.x());
          engineLive.rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress());
          engineLive.rootContext()->setContextProperty(QStringLiteral("start_url"), start_url);
      
          engineLive.load(QUrl(QLatin1String("qrc:/main.qml")));
      
          if (engineLive.rootObjects().isEmpty())
             return false;
      
          return true;
      }
      
      JonBJ 1 Reply Last reply
      0
      • C Collateral Studios

        I forgot to add .... in debug... when execute the load statement ...

        engineLive_1.load(QUrl(QLatin1String("qrc:/main.qml")));
        

        i get stuck again in

        QQmlApplicationEngine engineLive_1;
        

        and then proceeds to execute the next code.

        Also if i add the code in a function, and i try to call the function, i get the same result

        bool openWindow(int numScreen, QString start_url){
            QRect primaryScreen = QGuiApplication::screens()[numScreen-1]->availableGeometry();
            QQmlApplicationEngine engineLive;
        
            qInfo() << "Monitor Enabled:" << numScreen;
            qInfo() << "Monitor StartX:" << primaryScreen.x();
            qInfo() << "Monitor StartURL:" << start_url;
        
            engineLive.rootContext()->setContextProperty(QStringLiteral("startX"), primaryScreen.x());
            engineLive.rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress());
            engineLive.rootContext()->setContextProperty(QStringLiteral("start_url"), start_url);
        
            engineLive.load(QUrl(QLatin1String("qrc:/main.qml")));
        
            if (engineLive.rootObjects().isEmpty())
               return false;
        
            return true;
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Collateral-Studios
        I know absolutely nothing about QML, but with your if block or function your QQmlApplicationEngine engineLive goes out of scope and gets destroyed at the end of the block. Are you supposed to keep a QQmlApplicationEngine in scope after load till no longer wanted/exited for QML functioning??

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Collateral Studios
          wrote on last edited by
          #4

          Exactly, actually in debug i see that the qml is opened but it disappears as soon as the end of the if cycle. It is the same thing if i run the qml from a function.

          How can i keep the QML window/s open until the program is closed?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Why are you using a local QQmlApplicationEngine ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            C 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Why are you using a local QQmlApplicationEngine ?

              C Offline
              C Offline
              Collateral Studios
              wrote on last edited by Collateral Studios
              #6

              @SGaist
              I'm not sure why, i seem to remember that other solutions have given me problems on other platforms like Android.

              What would be the ideal solution? I simply need to open two or more qml windows if some occur

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Collateral Studios
                wrote on last edited by
                #7

                Ok... Fixed ... I had to use the QQuickView that seem to work well even within an if statement or a function.

                # code block
                bool openWindow(int numScreen, QString start_url){
                    QRect usedScreen = QGuiApplication::screens()[numScreen-1]->availableGeometry();
                
                    qInfo() << "Monitor Enabled:" << numScreen;
                    qInfo() << "Monitor StartX:" << usedScreen.x();
                    qInfo() << "Monitor StartURL:" << start_url;
                
                    QQuickView *view = new QQuickView(QUrl(QStringLiteral("qrc:/main.qml")));
                    view->setGeometry(usedScreen);
                    view->rootContext()->setContextProperty(QStringLiteral("startX"), usedScreen.x());
                    view->rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress());
                    view->rootContext()->setContextProperty(QStringLiteral("start_url"), start_url);
                    //view_1->showFullScreen();
                
                    //if (view->rootObjects().isEmpty())
                    //   return false;
                
                    return true;
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You have a memory leak here. You likely want to have the Qt::WA_DeleteOnClose attribute set on that widget other wise it won't be deleted.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved