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. Create sub a ListModel
QtWS25 Last Chance

Create sub a ListModel

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 3 Posters 6.0k 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.
  • M Offline
    M Offline
    Merinas
    wrote on last edited by
    #1

    Hi,

    I need to create a sub list of a ListModel, For example I have huge list of things and I want to get them by 10, like I could want a sub list which start width the 8th element and take 12 of them.

    What's the best way of doing that ?

    Thanks a lot.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I guess it depends on what you really need.

      My first hunch would be to subclass a QSortFilterProxyModel, and expose that with some relevant properties to QML to provide the data you need.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Merinas
        wrote on last edited by
        #3

        Thanks for answering. I need that : @
        ListModel{
        id:lmodel
        //[...]
        }

        SubListModel{
        id:lmodel1
        source:lmodel
        range.start:0
        range.lenght:10
        }

        ListView{
        model:lmodel1
        //[...]
        }@

        Is it possible with your suggestion ?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Not with just QML, no. My suggestion would take C++ to realize.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Merinas
            wrote on last edited by
            #5

            Ok, I get that, so subclass a QSortFilterProxyModel would solve my problem ?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              It could, if your data doesn't come from within QML. If you can get your data from the C++ side, you can use this approach. The problem is that you can not use a QSortFilterProxyModel on a model that comes from QML, but only on a QAbstractItemModel-derived model.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Merinas
                wrote on last edited by
                #7

                Arf... My data came from a QML ListModel since I work only with QML and some few C++plugins.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mario
                  wrote on last edited by
                  #8

                  Well, maybe you can do it by adding a javascript-function that will loop through your ListModel (lmodel) and add the desired item to other ListModel (lmodel1).

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Merinas
                    wrote on last edited by
                    #9

                    That's what I think of, but I'm not quite sure I can do that : @list.append(list2.get(i))@

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mario
                      wrote on last edited by
                      #10

                      Is this what you're looking for?

                      @
                      import QtQuick 1.0

                      Rectangle {
                      id: main

                      width: 400
                      height: 600
                      
                      ListModel {
                          id: myModel
                      
                          ListElement {
                              name: "element 0"
                          }
                          ListElement {
                              name: "element 1"
                          }
                          ListElement {
                              name: "element 2"
                          }
                          ListElement {
                              name: "element 3"
                          }
                      }
                      
                      ListModel {
                          id: subList
                      }
                      
                      
                      ListView {
                          id: listView
                      
                          anchors.fill: parent
                      
                          spacing: 8
                      
                          model: subList
                          delegate: Text { text: name; width: listView.width }
                      }
                      
                      Text {
                          text: "Update list"
                          anchors.right: parent.right; anchors.bottom: parent.bottom; anchors.margins: 5
                          MouseArea {
                              anchors.fill: parent
                              onClicked: updateSubList(1, 2)
                          }
                      }
                      
                      function updateSubList(begin, end) {
                          subList.clear()
                          end = Math.min(end, myModel.count-1)
                          for (var i = begin; i <= end ; i++) {
                              subList.append(myModel.get(i))
                          }
                      }
                      

                      }
                      @

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Merinas
                        wrote on last edited by
                        #11

                        Yeah that should do the tricks, thanks a lot.....
                        And what happen if the source model change ? Is there a signal emit somewhere, or did i need to build one ?

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mario
                          wrote on last edited by
                          #12

                          I actually tried connecting to a 'onCountChanged' signal on the ListModel but w/o success. Don't know if that is a bug or feature :)

                          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