02-13-2013 06:38 AM
Hello guys,
My app has a ListView and it's only shown after its dataSource is retrieved from the internet. The user is able to select only one item of the list at a time. I'm using a custom ListItemComponent that changes the background color if it's selected.
Everything all the UI is implemented in QML and only the data fetching is done in C++ and everything works just fine.
But now I want to set a selected item automatically once the ListView and its data are initialized. I've put this on my ListView but it doesn't work:
onCreationCompleted: {
console.debug("[ListView.onCreationCompleted] selected: "+isSelected(0) );
// The first item is selected by default.
clearSelection();
select(0,true);
}
onDataModelChanged: {
console.debug("[ListView.onDataModelChanged] selected: "+isSelected(0) );
// The first item is selected by default.
clearSelection();
select(0,true);
}
The ListView always starts with no selected item no matter what I do. The first time I trigger the item that supposed to be selected - the first item [0] - it is not selected yet.
Does anybody know a way to initialize a ListView with a selected item?
Thanks!
02-18-2013 04:19 AM
02-18-2013 04:36 AM
The documentation for ListView::select() is where you can find the answer
You need to pass an indexPath as the 1st parameter
02-18-2013 05:49 AM - edited 02-18-2013 05:50 AM
To get a ListView to position at a particular row, you need to specify the indexPath of the required row as follows:
lv.scrollToItem (_propMap.index, 0x0);
lv.select(_propMap.index,true);
scrollToItem() to scroll the listview to the selected index and select() to mark the row as selected.
_propMap.index is the indexPath of my rewuired element which i had determined from C++ side using model->find()
Alternatively you could hardcode the indexPath if u know it in advance. For instance,
lv.scrollToItem([0,4], 0x0);
lv.select([0,4], true)
would work just as fine only if i knew in advance that the indexPath of the required row is [0,4]
-Dishooom
![]()