How to set individual combo boxes currentindex?
-
Hello,I have some question about set the combobox currentindex.
I write some code like this
@
ListModel {id: author1} //data may append from other modelTableView {
model: author1
TableViewColumn{
role: “attribute”
title: “attribute”
width: 180
delegate:Rectangle {
ComboBox {
id:combo
model:author1
textRole: “attribute”
anchors.fill: parent
Component.onCompleted: {
var i
for(i=0;i<combo.count;i++)
combo.currentIndex=i
}
}
}
}
@Can I control each combobox in Component.onCompleted?
The code I wrote above can only set the last element to be currentindex.
I want to set each combobox currentindex according to the order in Listmodel.
i.e. the second combobox should set its currentindex to 1 (currentindex start from 0)Thanks!
-
The delegate is created per row so you do not want a loop in there.
What you are looking for is probably styleData.row which will give you the current row index of the delegate. Another issue is that you should not really rely on "onCompleted" for items in tableview delegates since the created instance can get recycled multiple times.
@
ComboBox {
id:combo
model:author1
textRole: "attribute"
anchors.fill: parent
currentIndex: styleData.row
}
@