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. How to Load Another Qml file?
Forum Updated to NodeBB v4.3 + New Features

How to Load Another Qml file?

Scheduled Pinned Locked Moved QML and Qt Quick
qt quickqml
3 Posts 2 Posters 7.3k 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.
  • I Offline
    I Offline
    iMasoud
    wrote on last edited by iMasoud
    #1

    Hi Guys,

    I have a simple Qt Quick project with 2 Qml files called "main.qml" and "Second.qml". when i run my app, the "main.cpp" loads and shows "main.qml".

    i want my app show "Second.qml", when user clicked text with id of "form_main_text" that is located in "main.qml", but i don't know how!

    Here are contents of "main.cpp":
    #include <QApplication>
    #include <QQmlApplicationEngine>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/Second.qml")));
    
    return app.exec();
    

    }

    Here are contents of "main.qml":
    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2

    ApplicationWindow
    {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    MainForm {
        id:form_main
        anchors.fill: parent
    
        Text
        {
            id:form_main_text
            anchors.centerIn: parent
            text:"This is Main Form!"
        }
    }
    

    }

    and finally content of "Second.qml":
    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2

    ApplicationWindow
    {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    MainForm {
        id:form_second
        anchors.fill: parent
    
        Text
        {
            id:form_second_text
            anchors.centerIn: parent
            text:"This is Second Form!"
        }
    }
    

    }

    thank you and sorry for my bad English.

    p3c0P 1 Reply Last reply
    0
    • I iMasoud

      Hi Guys,

      I have a simple Qt Quick project with 2 Qml files called "main.qml" and "Second.qml". when i run my app, the "main.cpp" loads and shows "main.qml".

      i want my app show "Second.qml", when user clicked text with id of "form_main_text" that is located in "main.qml", but i don't know how!

      Here are contents of "main.cpp":
      #include <QApplication>
      #include <QQmlApplicationEngine>

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/Second.qml")));
      
      return app.exec();
      

      }

      Here are contents of "main.qml":
      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtQuick.Window 2.2

      ApplicationWindow
      {
      title: qsTr("Hello World")
      width: 640
      height: 480
      visible: true

      MainForm {
          id:form_main
          anchors.fill: parent
      
          Text
          {
              id:form_main_text
              anchors.centerIn: parent
              text:"This is Main Form!"
          }
      }
      

      }

      and finally content of "Second.qml":
      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtQuick.Window 2.2

      ApplicationWindow
      {
      title: qsTr("Hello World")
      width: 640
      height: 480
      visible: true

      MainForm {
          id:form_second
          anchors.fill: parent
      
          Text
          {
              id:form_second_text
              anchors.centerIn: parent
              text:"This is Second Form!"
          }
      }
      

      }

      thank you and sorry for my bad English.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @iMasoud,

      You can do it in following ways:

      • Loader. Add this in main.qml and then set Second.qml as its source when required to load it.
      • Use Qt.createComponent() to load the QML. This is more dynamic approach. More info here.

      157

      I 1 Reply Last reply
      1
      • p3c0P p3c0

        Hi @iMasoud,

        You can do it in following ways:

        • Loader. Add this in main.qml and then set Second.qml as its source when required to load it.
        • Use Qt.createComponent() to load the QML. This is more dynamic approach. More info here.
        I Offline
        I Offline
        iMasoud
        wrote on last edited by
        #3

        Hi @p3c0 ,

        Thank you for your reply. It really was helpful.

        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