Change delegate properties afterwards.
-
Hello,
is there a way to change the properties of a delegate after a List is completed.
lets say I have a model like this
@ ListModel {
id: menu_model
ListElement {
index: 1
label: "foo"
}
ListElement {
index: 2
label: "bar"
}
}@And a ListView like this
@ ListView {
id:menu_list
anchors.fill: parent
model: menu_model
delegate: Item {
width: parent.width
height: 50 //this propertie must be changed dynamically
Rectangle {
anchors.fill: parent
border.color: "grey"
Text {
text: label
}
}
}@I need is to change the height of a specific Delegate according to other programm logik.
How can I access a specific list Item to change its properties ? -
Hey Schneidi,
yes it is possible. You can get an Item using:
@menu_model.get(i)@
To get the current index or an index at a Position you can use:
@menu_list.currentIndex()
menu_list.indexAt(x, y)@
You could also get an Item at a Position:
@menu_list.itemAt(x, y)@
To change it's properties just access it's property like you normaly would do:
@menu_model.get(i).label = "foobar"@Another method would be to iterate trough all children of your ListView and check for .index.
@for(var i = 0; i < menu_list.children.lenght(); i++)@
If the child has got a index it could access it, otherwise it would print "no property found" or something like that. Nothing important, it just couldn't access the property because it hasn't the property index. They you can check for a specific index like:
@if(menu_list.children[i].index == 2){
// Child with Index 2
}@Feel free to ask if you've got questions.
-
Hey onek24,
ok with this approach I access the properties of the model right ?
Is there also a way to change the height property of the delegate Item {}
like the one above ?
@height: 50 //this propertie must be changed dynamically@Or do I have to pass the height via the model and change the height in the model ?
-
[quote author="Schneidi" date="1392378277"]Hey onek24,
ok with this approach I access the properties of the model right ?[/quote]
Yes, you access the model OR you could access the item/children by itself. I provided the code for that above, too. But keep in mind: QML delegates are construkted and destroyed dynamically. Only visible delegates are existent.[quote author="Schneidi" date="1392378277"]
Is there also a way to change the height property of the delegate Item {}
like the one above ?
@height: 50 //this propertie must be changed dynamically@Or do I have to pass the height via the model and change the height in the model ?[/quote]
You can change the height of it using:
@menu_model.get(i).height = 50@