bigsleep
bigsleep

Reputation: 327

PyQt4: get list of all labels in QListWidget

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!

Upvotes: 3

Views: 11024

Answers (3)

uberdavis
uberdavis

Reputation: 11

Here is a solution using list comprehension:

labels = [list_widget.item(i).text() for i in range(list_widget.count())]

Upvotes: 1

Winand
Winand

Reputation: 2433

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

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

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1123420

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

Upvotes: 8

Related Questions