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. Accessing element in a separated Listmodel file
QtWS25 Last Chance

Accessing element in a separated Listmodel file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
listmodel
5 Posts 2 Posters 587 Views
  • 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.
  • A Offline
    A Offline
    Allon
    wrote on 25 Jul 2020, 05:30 last edited by
    #1

    Hi,
    I do not manage to access a given element from a Listmodel located in a separated file.

    What works is having the Listmodel inside the main file:

    main.qml:

    ListModel {
        id: taskData
    
        ListElement { name : "name1" }
        ListElement { name : "name2" }
    }
    
    Column {
        id: column
        Repeater {
            model: taskData
            Text { text: "I'm item " + taskData.get(1).name }
        }
    }
    

    It gives the following output:

    I'm item name2
    I'm item name2

    What I do not manage to access an element if the Listmodel in an external file.

    TaskData.qml

    import QtQuick 2.0
    
    ListModel {
        id: taskData
    
        property alias taskData: taskData
    
        ListElement { name : "name1" }
        ListElement { name : "name2" }
    }
    

    main.qml

    Window {
        visible: true
        width: 200
        height: 300
     
        Column {
            id: column
            Repeater {
                model: TaskData {}
                Text { text: "I'm item " + taskData.get(1).name }
            }
        }
    }
    

    When running the second example I get
    qrc:/main.qml:20: ReferenceError: taskData is not defined
    qrc:/main.qml:20: ReferenceError: taskData is not defined

    What should I do to be able to read taskData if it is present in an external file ?

    Thanks in advance,
    Emmanuel

    C 1 Reply Last reply 25 Jul 2020, 05:47
    0
    • A Allon
      25 Jul 2020, 06:07

      @Charby Hi,
      Is it not only the case when I am in the repeater?

      In my example I skimmed a large project where I am getting an index from a mousearea drag and drop and I want to access the element pointed by this index.

      Here is the original line of my program:

      taskData.get(taskColumnRectangleIndex).tasks.insert(taskData.get(taskColumnRectangleIndex).tasks.count, {"description": "task number: " + taskData.get(taskColumnRectangleIndex).tasks.count, /"taskHovered": true/ color:"red"})

      However in order to make the code a little bit more readable I wanted to move my Listmodel taskData out of the file I al coding in.

      Do you have an idea how I could then access taskData if it is located in a separated file?

      Thanks in advance,

      Emmanuel

      C Offline
      C Offline
      Charby
      wrote on 25 Jul 2020, 06:19 last edited by Charby
      #4

      @Allon just create TaskData item from your TaskData component :
      in main.qml

      Window {
          visible: true
          width: 200
          height: 300
       
          TaskData{
              id:taskData
          }
      
          Column {
              id: column
              Repeater {
                  model: taskData
                  Text { text: "I'm item " + name }
              }
          }
      }
      
      A 1 Reply Last reply 25 Jul 2020, 06:31
      0
      • A Allon
        25 Jul 2020, 05:30

        Hi,
        I do not manage to access a given element from a Listmodel located in a separated file.

        What works is having the Listmodel inside the main file:

        main.qml:

        ListModel {
            id: taskData
        
            ListElement { name : "name1" }
            ListElement { name : "name2" }
        }
        
        Column {
            id: column
            Repeater {
                model: taskData
                Text { text: "I'm item " + taskData.get(1).name }
            }
        }
        

        It gives the following output:

        I'm item name2
        I'm item name2

        What I do not manage to access an element if the Listmodel in an external file.

        TaskData.qml

        import QtQuick 2.0
        
        ListModel {
            id: taskData
        
            property alias taskData: taskData
        
            ListElement { name : "name1" }
            ListElement { name : "name2" }
        }
        

        main.qml

        Window {
            visible: true
            width: 200
            height: 300
         
            Column {
                id: column
                Repeater {
                    model: TaskData {}
                    Text { text: "I'm item " + taskData.get(1).name }
                }
            }
        }
        

        When running the second example I get
        qrc:/main.qml:20: ReferenceError: taskData is not defined
        qrc:/main.qml:20: ReferenceError: taskData is not defined

        What should I do to be able to read taskData if it is present in an external file ?

        Thanks in advance,
        Emmanuel

        C Offline
        C Offline
        Charby
        wrote on 25 Jul 2020, 05:47 last edited by Charby
        #2

        @Allon said in Accessing element in a separated Listmodel file:

        Text { text: "I'm item " + taskData.get(1).name }

        You don't need to provide an id and to to create a property for your listModel component,
        try instead to access the listmodel data directly with its role :

        Text { text: "I'm item " + name }
        

        you can disambiguate a role name using modelData (i.e modelData.name).

        A 1 Reply Last reply 25 Jul 2020, 06:07
        0
        • C Charby
          25 Jul 2020, 05:47

          @Allon said in Accessing element in a separated Listmodel file:

          Text { text: "I'm item " + taskData.get(1).name }

          You don't need to provide an id and to to create a property for your listModel component,
          try instead to access the listmodel data directly with its role :

          Text { text: "I'm item " + name }
          

          you can disambiguate a role name using modelData (i.e modelData.name).

          A Offline
          A Offline
          Allon
          wrote on 25 Jul 2020, 06:07 last edited by Allon
          #3

          @Charby Hi,
          Is it not only the case when I am in the repeater?

          In my example I skimmed a large project where I am getting an index from a mousearea drag and drop and I want to access the element pointed by this index.

          Here is the original line of my program:

          taskData.get(taskColumnRectangleIndex).tasks.insert(taskData.get(taskColumnRectangleIndex).tasks.count, {"description": "task number: " + taskData.get(taskColumnRectangleIndex).tasks.count, /"taskHovered": true/ color:"red"})

          However in order to make the code a little bit more readable I wanted to move my Listmodel taskData out of the file I al coding in.

          Do you have an idea how I could then access taskData if it is located in a separated file?

          Thanks in advance,

          Emmanuel

          C 1 Reply Last reply 25 Jul 2020, 06:19
          0
          • A Allon
            25 Jul 2020, 06:07

            @Charby Hi,
            Is it not only the case when I am in the repeater?

            In my example I skimmed a large project where I am getting an index from a mousearea drag and drop and I want to access the element pointed by this index.

            Here is the original line of my program:

            taskData.get(taskColumnRectangleIndex).tasks.insert(taskData.get(taskColumnRectangleIndex).tasks.count, {"description": "task number: " + taskData.get(taskColumnRectangleIndex).tasks.count, /"taskHovered": true/ color:"red"})

            However in order to make the code a little bit more readable I wanted to move my Listmodel taskData out of the file I al coding in.

            Do you have an idea how I could then access taskData if it is located in a separated file?

            Thanks in advance,

            Emmanuel

            C Offline
            C Offline
            Charby
            wrote on 25 Jul 2020, 06:19 last edited by Charby
            #4

            @Allon just create TaskData item from your TaskData component :
            in main.qml

            Window {
                visible: true
                width: 200
                height: 300
             
                TaskData{
                    id:taskData
                }
            
                Column {
                    id: column
                    Repeater {
                        model: taskData
                        Text { text: "I'm item " + name }
                    }
                }
            }
            
            A 1 Reply Last reply 25 Jul 2020, 06:31
            0
            • C Charby
              25 Jul 2020, 06:19

              @Allon just create TaskData item from your TaskData component :
              in main.qml

              Window {
                  visible: true
                  width: 200
                  height: 300
               
                  TaskData{
                      id:taskData
                  }
              
                  Column {
                      id: column
                      Repeater {
                          model: taskData
                          Text { text: "I'm item " + name }
                      }
                  }
              }
              
              A Offline
              A Offline
              Allon
              wrote on 25 Jul 2020, 06:31 last edited by
              #5

              @Charby It works like a charm!
              Thanks you very much, I have been trying to solve this problem since hours!
              I would not have found it myself.
              Thanks again,
              Emmanuel

              1 Reply Last reply
              0

              5/5

              25 Jul 2020, 06:31

              • Login

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