Reputation: 145
I've got a QListWidget with extended selection enabled self.sensors.setSelectionMode(QAbstractItemView.ExtendedSelection)
.
To get the text of all selected Items the following works:
for item in self.sensors.selectedItems():
target.write(" "+item.text()+",")
The output however is sorted by order of selection. Is there a quick way to get the items sorted by row number? I can get the row of an item using QListWidget.row(self.sensors, item)
.
Upvotes: 2
Views: 528
Reputation: 3945
# create a dict containing index and corresponding item
tempDict = {}
for item in self.sensors.selectedItems():
tempDict[self.sensors.row(item)] = item
# sort the index and store as a list (`sorted()` does this for you)
tempIndexes = sorted(tempDict)
# define a list to contain the resultant items i.e sorted items
resultItems = []
for index in tempIndexes:
resultItems.append(tempDict[index])
for it in resultItems:
print(it.text())
Upvotes: 3