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. Access to DelegateModel items properties

Access to DelegateModel items properties

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 2.0k Views 1 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.
  • deleted57D Offline
    deleted57D Offline
    deleted57
    wrote on last edited by
    #1

    Hi all

    I have GridView control managed by a DelegateModel. I need to access to the GridView items properties but I can not find a way to get the item object. Something like:

    GridView {
    	cellWidth: 80
    	cellHeight: 80
    
    	model: DelegateModel {
    		id: myDelegateModel
    		delegate: MyItemDelegate {}
    		model: myModel
    	}
    }
    

    The MyItemDelegate is basically:

    Rectangle {
    	width: 50
    	height: 50
    
    	property int myProperty: 0
    }
    

    Supposing I have in the model a list of 10 items how is possible to read the MyProperty value of the items 4 showed into the GridView for example?

    DiracsbracketD 1 Reply Last reply
    0
    • deleted57D deleted57

      Hi all

      I have GridView control managed by a DelegateModel. I need to access to the GridView items properties but I can not find a way to get the item object. Something like:

      GridView {
      	cellWidth: 80
      	cellHeight: 80
      
      	model: DelegateModel {
      		id: myDelegateModel
      		delegate: MyItemDelegate {}
      		model: myModel
      	}
      }
      

      The MyItemDelegate is basically:

      Rectangle {
      	width: 50
      	height: 50
      
      	property int myProperty: 0
      }
      

      Supposing I have in the model a list of 10 items how is possible to read the MyProperty value of the items 4 showed into the GridView for example?

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #2

      Hi @Suppaman
      Have a look at:
      https://stackoverflow.com/questions/9039497/how-to-get-an-instantiated-delegate-component-from-a-gridview-or-listview-in-qml

      1 Reply Last reply
      1
      • deleted57D Offline
        deleted57D Offline
        deleted57
        wrote on last edited by
        #3

        Hi
        Thank you for your reply. I already followed the way to use contentItem.children but it seem not all the children object are my delegates. I created a gridview and I expected to see ordered into contentItem.children all the delegated showed into the view. However, I don't know why, the object at position 1 was a generic QtQuick object instead of one of my delegates at view position 1. However it seem the most correct way to proceed than I'll try to investigate more...

        DiracsbracketD 1 Reply Last reply
        0
        • deleted57D deleted57

          Hi
          Thank you for your reply. I already followed the way to use contentItem.children but it seem not all the children object are my delegates. I created a gridview and I expected to see ordered into contentItem.children all the delegated showed into the view. However, I don't know why, the object at position 1 was a generic QtQuick object instead of one of my delegates at view position 1. However it seem the most correct way to proceed than I'll try to investigate more...

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by
          #4

          @Suppaman
          Qt's views indeed do not provide an easy way to access its delegates by (model) index, mainly because , by default, not all delegates are instantiated at all times. This is unlike the Repeater, which instantiates all delegates at creation time and which provides the itemAt(index) method.

          The best thing is probably not to have any properties defined in your delegate but instead define them in your model. Alas, this is not always possible, e.g. if your model is read-only.

          Apart from that, you can set the cacheBuffer property of your view to an appropriate value to ensure that all delegates will be instantiated at all times; for large models however this may not be economical.

          Even so, the question then still remains of how to retrieve only the delegates from contentItem.children based on their model index and ignore other, non-delegate children. Instead of looping through all children to check their object type, you could add the instantiated delegates to a list (e.g. in the Component.onCompleted handler of the delegate).

          Good luck.

          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