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. Push StackView from C++
Forum Updated to NodeBB v4.3 + New Features

Push StackView from C++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 746 Views 2 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.
  • T Offline
    T Offline
    therealmatiss
    wrote on 28 Apr 2022, 12:58 last edited by therealmatiss
    #1

    Hello!

    I have to push QML StackView screens from different parts of C++ function - what could be the best way to do it? Why does it say here https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html that manipulating GUI from C++ is a bad idea? I literally have a GUI that's dependent from the results of my C++ program.

    This is what I have so far:

      QQmlApplicationEngine engine;
      const QUrl url(u"qrc:/Example/qml/screen_start.qml"_qs);
      QObject::connect(
          &engine, &QQmlApplicationEngine::objectCreated, &app,
          [url](QObject* obj, const QUrl& objUrl) {
            if (!obj && url == objUrl)
              QCoreApplication::exit(-1);
          },
          Qt::QueuedConnection);
         engine.load(url);
    
    ...
    
      QObject *rootObject = engine.rootObjects().first();
      QObject *qmlObject = rootObject->findChild<QObject*>("mainStackview");
      const QUrl screenurl(u"qrc:/Example/qml/screen_done.qml"_qs);
      if (!QMetaObject::invokeMethod(qmlObject, "push", Q_ARG(QUrl, screenurl))) {
        SPDLOG_ERROR("Failed to push Stackview");
      }
    
    J 1 Reply Last reply 29 Apr 2022, 11:41
    0
    • F Offline
      F Offline
      fcarney
      wrote on 28 Apr 2022, 21:29 last edited by
      #2

      @therealmatiss said in Push StackView from C++:

      I literally have a GUI that's dependent from the results of my C++ program.

      Just like every other program does.

      Learn how Qt models work. https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        therealmatiss
        wrote on 29 Apr 2022, 11:31 last edited by
        #3

        There's literally nothing there about C++. I can't control everything from QML. I just have to know, how to send push command properly to the StackView from C++ code.

        F 1 Reply Last reply 29 Apr 2022, 15:11
        0
        • T therealmatiss
          28 Apr 2022, 12:58

          Hello!

          I have to push QML StackView screens from different parts of C++ function - what could be the best way to do it? Why does it say here https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html that manipulating GUI from C++ is a bad idea? I literally have a GUI that's dependent from the results of my C++ program.

          This is what I have so far:

            QQmlApplicationEngine engine;
            const QUrl url(u"qrc:/Example/qml/screen_start.qml"_qs);
            QObject::connect(
                &engine, &QQmlApplicationEngine::objectCreated, &app,
                [url](QObject* obj, const QUrl& objUrl) {
                  if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
                },
                Qt::QueuedConnection);
               engine.load(url);
          
          ...
          
            QObject *rootObject = engine.rootObjects().first();
            QObject *qmlObject = rootObject->findChild<QObject*>("mainStackview");
            const QUrl screenurl(u"qrc:/Example/qml/screen_done.qml"_qs);
            if (!QMetaObject::invokeMethod(qmlObject, "push", Q_ARG(QUrl, screenurl))) {
              SPDLOG_ERROR("Failed to push Stackview");
            }
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 29 Apr 2022, 11:41 last edited by
          #4

          @therealmatiss said in Push StackView from C++:

          Why does it say here ... that manipulating GUI from C++ is a bad idea

          What it says, is, that its a bad idea to fetch a pointer to a GUI component and than directly manipulating those elements.

          What you're supposed to do is expose a c++ class/instance to QML
          Either as a context property, as a singleton or as in instantiable QML component


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • G Offline
            G Offline
            GrecKo
            Qt Champions 2018
            wrote on 29 Apr 2022, 12:12 last edited by
            #5

            Emit a signal in C++, like userLogged.

            in QML do something like the following :

            Connections {
                target: YourCppObject
                function onUserLogged() {
                    yourStackView.push("YourLoggedInPage.qml");
                }
            }
            
            1 Reply Last reply
            1
            • T therealmatiss
              29 Apr 2022, 11:31

              There's literally nothing there about C++. I can't control everything from QML. I just have to know, how to send push command properly to the StackView from C++ code.

              F Offline
              F Offline
              fcarney
              wrote on 29 Apr 2022, 15:11 last edited by
              #6

              @therealmatiss said in Push StackView from C++:

              There's literally nothing there about C++.

              https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#c-data-models

              The first link (same page as this link) gives you an overview of how to present data to qml. The sublink shows how this relates to data models and shows how to expose those models to qml from c++. Having a good understanding of the model view is essential for using qml and c++ effectively together.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0

              1/6

              28 Apr 2022, 12:58

              • Login

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