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. Use the Repeater element to Create a Listmodel with various ListElements
Qt 6.11 is out! See what's new in the release blog

Use the Repeater element to Create a Listmodel with various ListElements

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 729 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.
  • S Offline
    S Offline
    smeik
    wrote on last edited by
    #1

    Hi,
    I hope someone can Help me. I have a ListModel and I want to fill it automatically with
    ListElements. All the ListElements have a name and an info property. I also have two Arrays that store the name and info values.
    Is there a way to do this? maybe by using the Repeater?

    Thanks in Advance for helping me.

    GrecKoG 1 Reply Last reply
    0
    • S smeik

      Hi,
      I hope someone can Help me. I have a ListModel and I want to fill it automatically with
      ListElements. All the ListElements have a name and an info property. I also have two Arrays that store the name and info values.
      Is there a way to do this? maybe by using the Repeater?

      Thanks in Advance for helping me.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @smeik why do you want to do that?

      You can use your arrays directly as models without ListModel, they won't have any roles though.

      fcarneyF S 2 Replies Last reply
      1
      • GrecKoG GrecKo

        @smeik why do you want to do that?

        You can use your arrays directly as models without ListModel, they won't have any roles though.

        fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @GrecKo I use dictionaries inside lists. So my "roles" look like this:

        [
        {name: "Fred"}
        ]
        ...
        modelData.name
        

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • GrecKoG GrecKo

          @smeik why do you want to do that?

          You can use your arrays directly as models without ListModel, they won't have any roles though.

          S Offline
          S Offline
          smeik
          wrote on last edited by
          #4

          @GrecKo
          Thanks. Do you have an example how to do this?

          GrecKoG 1 Reply Last reply
          0
          • RusticusR Offline
            RusticusR Offline
            Rusticus
            wrote on last edited by
            #5

            You can also use some JavaScript to fill your ListModel

            function filllListModel()
            {
              for(int i = 0; i < JavaScriptArrayName.length; i++)
              {
                myListModel.append(  JavaScriptArrayName[i]  );
              }
            }
            
            1 Reply Last reply
            0
            • S smeik

              @GrecKo
              Thanks. Do you have an example how to do this?

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              @smeik something like this :

              ListView {
                  model: [
                      { foo: "hello", bar: "red" },
                      { foo: "world", bar: "orange" }
                  ]
                  delegate: ItemDelegate {
                      text: modelData.foo
                      color: modelData.bar
                  }
              }
              

              Alternatively you could use JsonListModel from https://github.com/benlau/qsyncable if you want to transform a js array to a proper QAbstractListModel

              JsonListModel {
                  id: myModel
                  source: [
                      { foo: "hello", bar: "red" },
                      { foo: "world", bar: "orange" }
                  ]
              }    
              ListView {
                  model: myModel
                  delegate: ItemDelegate {
                      text: model.foo
                      color: model.bar
                  }
              }
              
              1 Reply Last reply
              1

              • Login

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