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. QML - How to add into a qml file two different qml files, based on condition. Is it posible?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 2.1k Views 3 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.
  • F Offline
    F Offline
    Flavian
    wrote on last edited by
    #1

    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.

    p3c0P kshegunovK 3 Replies Last reply
    0
    • F Flavian

      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.

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

      @Flavian
      Have a look at this document.

      157

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Flavian
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • tekojoT Offline
          tekojoT Offline
          tekojo
          wrote on last edited by
          #4

          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
          0
          • F Flavian

            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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @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
            0
            • F Flavian

              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.

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

              @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
              0
              • F Offline
                F Offline
                Flavian
                wrote on last edited by
                #7

                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!

                p3c0P 1 Reply Last reply
                0
                • F Flavian

                  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!

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

                  @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
                  0
                  • F Offline
                    F Offline
                    Flavian
                    wrote on last edited by
                    #9

                    You are right, thank you!

                    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