PyQt4: get list of all labels in QListWidget

10,140

Solution 1

.text() returns the text within a QListWidgetItem. Note that you need to call .item(index) on the original QListWidget instance to get the items contained in the list widget:

items = []
for index in xrange(self.ui.QListWidget.count()):
     items.append(self.ui.QListWidget.item(index))
labels = [i.text() for i in items]

Solution 2

You can force list widget to return all items with findItems:

lst = [i.text() for i in self.lstFiles.findItems("", QtCore.Qt.MatchContains)]

Solution 3

Here is a solution using list comprehension:

labels = [list_widget.item(i).text() for i in range(list_widget.count())]
Share:
10,140
bigsleep
Author by

bigsleep

Updated on June 28, 2022

Comments

  • bigsleep
    bigsleep almost 2 years

    I am new to PyQt4 and especially the QListWidget. I am trying to get a (Python) list of of all labels currently displayed in the QListWidget. I'm able to to get a list of all the QListWidgetItems, but I'm not sure how to get to the labels from there...

    This is what I use to get the list of all the QListWidgetItems:

        items = []
        for index in xrange(self.ui.QListWidget.count()):
             items.append(self.ui.QListWidgetitem(index))
    

    Thanks for your help!