PyQt4:获取 QListWidget 中所有标签的列表 [英] PyQt4: get list of all labels in QListWidget
问题描述
我是 PyQt4 的新手,尤其是 QListWidget.我正在尝试获取当前显示在 QListWidget 中的所有标签的(Python)列表.我能够获得所有 QListWidgetItems 的列表,但我不确定如何从那里获得标签...
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...
这是我用来获取所有 QListWidgetItems 的列表:
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))
感谢您的帮助!
推荐答案
.text()
返回 QListWidgetItem 中的文本.请注意,您需要在原始 QListWidget 实例上调用 .item(index)
以获取包含在列表小部件中的项目:
.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]
这篇关于PyQt4:获取 QListWidget 中所有标签的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!