简体   繁体   中英

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!

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

您可以强制列表小部件使用findItems返回所有项目:

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

这是使用列表理解的解决方案:

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

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