Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QML - How to add into a qml file two different qml files, based on condition. Is it posible?

    QML and Qt Quick
    4
    9
    1349
    Loading More Posts
    • 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.
    • F
      Flavian last edited by

      Say we have : main.qml

      And I want to add first.qml, or second.qml with the same id (but different contents) inside an item {

      } from main.qml, based on a condition.

      p3c0 kshegunov 3 Replies Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Flavian last edited by

        @Flavian
        Have a look at this document.

        157

        1 Reply Last reply Reply Quote 0
        • F
          Flavian last edited by

          It didn't helped me, I tried with an Loader, but didn't get expected results.

          1 Reply Last reply Reply Quote 0
          • tekojo
            tekojo last edited by

            Maybe this page then?
            http://doc.qt.io/qt-5/qml-qtquick-loader.html
            It's more recent, so should work better.

            1 Reply Last reply Reply Quote 0
            • kshegunov
              kshegunov Moderators @Flavian last edited by kshegunov

              @Flavian said:

              And I want to add first.qml, or second.qml with the same id

              Is this - having two objects with the same id, allowed at all? I don't use QML but I think the id should be unique, shouldn't it?

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply Reply Quote 0
              • p3c0
                p3c0 Moderators @Flavian last edited by p3c0

                @Flavian What didn't work ? Its plain simple. The component name is actually the file name. They are available readily if its in the same path.

                //File1
                import QtQuick 2.0
                
                Rectangle {
                    id: rect
                    color: "green"
                    width: 40
                    height: 40
                }
                
                //File2
                import QtQuick 2.0
                
                Rectangle {
                    id: rect //same id as File1
                    color: "red"
                    width: 40
                    height: 40
                }
                
                //main.qml
                import QtQuick 2.0
                
                Item {
                    width: 200
                    height: 200
                
                    File1 {
                        x: 0
                    }
                
                    File2 {
                        x: 100
                    }
                }
                
                

                @kshegunov Not a problem if they are in different file. It would conflict if its in the same file just like some public member in C++.

                157

                1 Reply Last reply Reply Quote 0
                • F
                  Flavian last edited by

                  Hello everyone, thank you for you kind answers, I was hoping for something like this:
                  Item{

                  Loader {
                  //id:loader
                  source: (condition ? "File1.qml" : "File2.qml")
                  }
                  }

                  And rely on the loader's id, or the id from the file1.qml , or file2.qml(in my case identical)-> it didn't provide the functionality, I really didn't access directly File1.qml (like loader.property -> or directly with the id from inside File1.qml . It seemed a little overhead to try loader.id(File.qml).

                  After of hours of trial and error, i have ended in using both files:
                  Item{
                  File1{}
                  File2{}

                  }
                  And adapting them with different calls, as needed.

                  Thank you again for you kind effort, you are the best!

                  p3c0 1 Reply Last reply Reply Quote 0
                  • p3c0
                    p3c0 Moderators @Flavian last edited by

                    @Flavian The Loader method will work too. To access component loaded inside Loader you need to use Loader.item property.

                    157

                    1 Reply Last reply Reply Quote 0
                    • F
                      Flavian last edited by

                      You are right, thank you!

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post