Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loop through the delegate of ListView

Loop through the delegate of ListView

Scheduled Pinned Locked Moved General and Desktop
listviewdelegatemodel
5 Posts 3 Posters 6.8k 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.
  • E Offline
    E Offline
    Erakis
    wrote on last edited by
    #1

    Hi,

    First question :
    Is there a way to loop through all delegates of a ListView ? I noticed that Listview is having a method named itemAt but it take (x,y) parameters. This is useful if we are using a MouseArea to get the item from a mouse position. But what if we want to scan the Listview item to find a specific item or gather a group of item that match a predicate ?

    Second question :
    Does the model item associated with the Listview are always in the same order as the delegates ?

    Best regards,

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

      If I understand right you probably will need to loop through every model index with
      QAbstractItemDelegate * QAbstractItemView::itemDelegate ( const QModelIndex & index ) const

      1 Reply Last reply
      0
      • E Erakis

        Hi,

        First question :
        Is there a way to loop through all delegates of a ListView ? I noticed that Listview is having a method named itemAt but it take (x,y) parameters. This is useful if we are using a MouseArea to get the item from a mouse position. But what if we want to scan the Listview item to find a specific item or gather a group of item that match a predicate ?

        Second question :
        Does the model item associated with the Listview are always in the same order as the delegates ?

        Best regards,

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

        Hi @Erakis,

        s there a way to loop through all delegates of a ListView ? I noticed that Listview is having a method named itemAt but it take (x,y) parameters. This is useful if we are using a MouseArea to get the item from a mouse position. But what if we want to scan the Listview item to find a specific item or gather a group of item that match a predicate ?

        You will need to iterate over the children of ListView's contentItem
        eg:

        ListView {
            id: list
            width: 200; height: 40
            model: mymodel
            delegate: Text {
                objectName: "text"
                text: name + ": " + number
            }
        }
        
        for(var child in list.contentItem.children) {
            console.log(list.contentItem.children[child].objectName)
        }
        

        You can then filter using objectName or any other property of the delegate Item.

        Does the model item associated with the Listview are always in the same order as the delegates ?

        AFAIK, yes. Delegates are always in the same order as of the model items.

        157

        1 Reply Last reply
        1
        • E Offline
          E Offline
          Erakis
          wrote on last edited by
          #4

          Thank you for your answers.

          So if the Delegates are always in the same order as of the model items I could also loop through the model items as long as they are all visible (like alex_malyy said) ? But what If I add a new children dynamically to the Listview, (without using the model) it will not be available in the model data.

          I think the best way for me will be to loop through the children and get the data associated to this Delegate. But how to get the data associated to this Delegates ? Do I have to expose a property like this

           Component {
                property variant myData: myListModel.get(index)
           }
          

          to my Delegates ?

          and using it like this ?

          for(var child in list.contentItem.children) {
              console.log(list.contentItem.children[child].myData)
          }
          

          It seem to work, but it is the right way ?

          Best regards,

          p3c0P 1 Reply Last reply
          0
          • E Erakis

            Thank you for your answers.

            So if the Delegates are always in the same order as of the model items I could also loop through the model items as long as they are all visible (like alex_malyy said) ? But what If I add a new children dynamically to the Listview, (without using the model) it will not be available in the model data.

            I think the best way for me will be to loop through the children and get the data associated to this Delegate. But how to get the data associated to this Delegates ? Do I have to expose a property like this

             Component {
                  property variant myData: myListModel.get(index)
             }
            

            to my Delegates ?

            and using it like this ?

            for(var child in list.contentItem.children) {
                console.log(list.contentItem.children[child].myData)
            }
            

            It seem to work, but it is the right way ?

            Best regards,

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

            @Erakis

            But what If I add a new children dynamically to the Listview, (without using the model) it will no...

            You have to add item to the model so that it reflects in ListView not directly.

            I think the best way for me will be to loop through the children and get the data associated to this Delegate. But how to get the data associated to this Delegates ?

            To get the data always use the model instead of delegate. In the first question in your first post you asked for delegate so I told the way to iterate over the delegate. To get the data its good to use model.

            157

            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