简体   繁体   中英

QListWidget selected items orderd by row

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) .

# 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM