How can I get the indices of QListWidget::selectedItems()?

user3051029 picture user3051029 · Jun 2, 2016 · Viewed 7.8k times · Source

I have QListWidget and I need to get the indices of selected items. (I could work with items in the list by values, but I have std::vector that contains some objects for each item in the list and I also need to delete them from it.) There is a fucntion indexFromItem but it's protected(?) so I can't use it.

Any ideas?

QList<QListWidgetItem *> itemList = lw1->selectedItems();

Answer

skypjack picture skypjack · Jun 2, 2016

You can use maybe:

list->selectionModel()->selectedIndexes()

Where list is an instance of QListWidget.
Not sure if it returns exactly what you are looking for.

From the documentation:

Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.

QListWidget has a method named selectedIndexes as well, but it is a protected one, so you have to use such a tricky way to get them.