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. Setting Up StackView's initialItem property from c++.
Forum Updated to NodeBB v4.3 + New Features

Setting Up StackView's initialItem property from c++.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 1.2k 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.
  • G Offline
    G Offline
    Gurubaran
    wrote on last edited by Gurubaran
    #1

    I want to set up the initialItem of StackView from c++,
    This is my code
    main.cpp
    @
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    QObject *rootObject = engine.rootObjects().first();
    QObject stack = rootObject->findChild<QObject>("pageStack");
    if(stack)
    {
    stack->setProperty("initialItem" , "qrc:/newPage.qml");
    }
    qDebug() << "initialItem :" << stack->property("initialItem").toString(); **/prints output as - > initialItem : "qrc:/newPage.qml" /
    if(engine.rootObjects().isEmpty())
    return -1;

    return app.exec();
    

    }
    @
    main.qml
    @
    import QtQuick 2.9
    import QtQuick.Controls 2.2
    import QtQuick.Layouts 1.3

    ApplicationWindow {
    id: mainWindow
    visible: true
    width: 1024
    height: 768

    Rectangle {
        color: "#ffffff"
        anchors.fill: parent
        objectName: "rect"
    
    }
    StackView {
        id: stackView
        objectName: "pageStack"
        anchors.fill: parent
    }
    

    }
    @

    The property is getting set (from the debug message) but it is not moving to that specified url. Please help understand what i am doing wrong. Thanks in advance

    JonBJ 1 Reply Last reply
    0
    • G Gurubaran

      I want to set up the initialItem of StackView from c++,
      This is my code
      main.cpp
      @
      int main(int argc, char *argv[])
      {
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;
      engine.load(QUrl(QLatin1String("qrc:/main.qml")));
      QObject *rootObject = engine.rootObjects().first();
      QObject stack = rootObject->findChild<QObject>("pageStack");
      if(stack)
      {
      stack->setProperty("initialItem" , "qrc:/newPage.qml");
      }
      qDebug() << "initialItem :" << stack->property("initialItem").toString(); **/prints output as - > initialItem : "qrc:/newPage.qml" /
      if(engine.rootObjects().isEmpty())
      return -1;

      return app.exec();
      

      }
      @
      main.qml
      @
      import QtQuick 2.9
      import QtQuick.Controls 2.2
      import QtQuick.Layouts 1.3

      ApplicationWindow {
      id: mainWindow
      visible: true
      width: 1024
      height: 768

      Rectangle {
          color: "#ffffff"
          anchors.fill: parent
          objectName: "rect"
      
      }
      StackView {
          id: stackView
          objectName: "pageStack"
          anchors.fill: parent
      }
      

      }
      @

      The property is getting set (from the debug message) but it is not moving to that specified url. Please help understand what i am doing wrong. Thanks in advance

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Gurubaran
      I know nothing about QML, but your property name has space-colon on the end of it:

      stack->setProperty("initialItem :" , "qrc:/newPage.qml");

      I would have thought it should be just initialItem, as in your stack->property("initialItem").toString(), no?

      G 1 Reply Last reply
      0
      • JonBJ JonB

        @Gurubaran
        I know nothing about QML, but your property name has space-colon on the end of it:

        stack->setProperty("initialItem :" , "qrc:/newPage.qml");

        I would have thought it should be just initialItem, as in your stack->property("initialItem").toString(), no?

        G Offline
        G Offline
        Gurubaran
        wrote on last edited by Gurubaran
        #3

        I moved the content of main.qml to MainWindow.qml and implemented a Loader qml type with a Timer qml type (to give a small delay before loading the first qml page) on main.qml, this solved the problem.

        If there is any better Solution please let me know .
        Thank You.

        T 1 Reply Last reply
        0
        • G Gurubaran

          I moved the content of main.qml to MainWindow.qml and implemented a Loader qml type with a Timer qml type (to give a small delay before loading the first qml page) on main.qml, this solved the problem.

          If there is any better Solution please let me know .
          Thank You.

          T Offline
          T Offline
          Tom_H
          wrote on last edited by
          #4

          @Gurubaran
          Consider:

          Component.onCompleted: {
              stackView.push("newPage.qml")
          }
          
          GrecKoG 1 Reply Last reply
          0
          • T Tom_H

            @Gurubaran
            Consider:

            Component.onCompleted: {
                stackView.push("newPage.qml")
            }
            
            GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #5

            @Tom_H said in Setting Up StackView's initialItem property from c++.:

            @Gurubaran
            Consider:

            Component.onCompleted: {
                stackView.push("newPage.qml")
            }
            

            That's a more verbose way to write initialItem: "newPage.qml".

            T 1 Reply Last reply
            0
            • GrecKoG GrecKo

              @Tom_H said in Setting Up StackView's initialItem property from c++.:

              @Gurubaran
              Consider:

              Component.onCompleted: {
                  stackView.push("newPage.qml")
              }
              

              That's a more verbose way to write initialItem: "newPage.qml".

              T Offline
              T Offline
              Tom_H
              wrote on last edited by
              #6

              @GrecKo
              Yes, I didn't understand why he wanted to do it from C++ or then use a timer. But of course you are right.

              1 Reply Last reply
              0

              • Login

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