I figured this out… but my solution was pretty nasty. The only way to ensure the right delegate was selected was to re-check which one was selected every time the layout changed (since the layout obviously has to change if the items are re-sorted).
Below is a snippet of what I did. There has to be a better way!
Note that “modelId” is a role on the model.
Component
{
id: myDelegate
Column
{
id: column
Component.onCompleted:
{
myListView.setSelected.connect(handleSetSelected)
}
// If we don't disconnect from the signal the handler will still be called
// on destroyed components and errors will be thrown about undefined
// variables.
Component.onDestruction:
{
myListView.setSelected.disconnect(handleSetSelected)
}
function setSelected()
{
myListView.setCurrentItem(index, modelId);
}
function handleSetSelected(modelIdToBeSelected)
{
if (modelId === modelIdToBeSelected)
{
setSelected();
}
}
// More delegate stuff...
}
}
ListView
{
property int currentModelId: 0
signal setSelected(int modelId)
function setCurrentItem(index, modelId)
{
currentIndex = index;
currentModelId = modelId;
}
id: myListView
objectName: "MyList"
model: myModel
delegate: myDelegate
// Signal handlers
Connections
{
target: myModel
onLayoutChanged:
{
myListView.setSelected(myListView.currentModelId)
}
}
}
↧