我道歉,如果已经被问过,但请你再次澄清一下:如何获取QListWidget中列出的所有ListWidgetItems?
这是在行动中。列表中有5个项目。减去一个结果4。
from PyQt4 import QtGui, QtCore
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.lw = QtGui.QListWidget()
myBoxLayout.addWidget(self.lw)
for i in range(5):
QtGui.QListWidgetItem('myItem', self.lw)
ok_button = QtGui.QPushButton("Print count")
ok_button.clicked.connect(self.OK)
myBoxLayout.addWidget(ok_button)
def OK(self):
# let self.lw haven elements in it.
items = []
for x in range(self.lw.count()-1):
items.append(self.lw.item(x))
print len(items)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
sys.exit(app.exec_())
答案 0 :(得分:3)
这是一种在listWidget中获取所有ListWidgetItem的简单方法。
lw = QtGui.QListWidget()
# let lw haven elements in it.
items = []
for x in range(lw.count()-1):
items.append(lw.item(x))
#items will consist a list of ListWidgetItems.
答案 1 :(得分:1)
这是实现这一点的pythonic方法:
lw = QtGui.QListWidget()
items = [lw.item(x) for x in range(lw.count())]
或者如果你想要一个字符串列表:
lw = QtGui.QListWidget()
items = [lw.item(x).text() for x in range(lw.count())]
答案 2 :(得分:0)
从QlistWidget对象中提取值
def Extract(self):
lst = QtGui.QListWidget()
items = []
for x in range(lst.count()):
items.append(lst.item(x).text())
print(items)