PyQt4: get list of all labels in QListWidget

倾然丶 夕夏残阳落幕 提交于 2019-12-12 13:16:57

问题


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!


回答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]



回答2:


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

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


来源:https://stackoverflow.com/questions/12087715/pyqt4-get-list-of-all-labels-in-qlistwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!