假设您创建了一个用户界面文件myDialog.ui
与设计师。 QListWidget和QComboBox被称为comboBox
和listWidget
。 要更新您的QListWidget,我提出了这种方法:
#!/usr/bin/env python
# -*- coding: utf-8 *-
import sys
import os
from PyQt4 import QtGui, QtCore, uic
app = QtGui.QApplication(sys.argv)
class MyDialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
uic.loadUi(os.path.join(os.path.dirname(os.path.abspath(__file__)),"myDialog.ui"), self)
self.comboBox.currentIndexChanged.connect(self.updateList)
self.comboBox.clear()
self.comboBox.insertItems(0,self.getFilters())
self.updateList()
pass
def updateList(self):
items = self.getListItems()
text_filter = str(self.comboBox.currentText())
self.listWidget.clear()
# If "All" is used, no filter is applied
self.listWidget.insertItems(0,[text for text in items if text_filter in text + "All"])
pass
def getFilters(self):
# Write here your own method to retrieve the filters
return ["Yellow", "Blue", "Red", "All"]
def getListItems(self):
# Write here your own method to retrieve the list values
return ["Yellow 01", "Yellow 02", "Blue 01", "Red 01", "Red 02", "Red 03 "]
myDialog = MyDialog()
myDialog.show()
sys.exit(app.exec_())
我不熟悉Python。 你可以用字符串作为键和字符串列表的值作为值吗?然后,您可以使用选定的字符串并显示相应的值... –
您好,我并没有真正明白您的意思......我对Python语言也很新颖。嗯,你可以详细解释一下吗? – yan
在python中它叫[dictionary](http://docs.python.org/2/library/stdtypes.html#typesmapping)。关键是'QString',值是'QVector'。然后,您可以将组合框字符串作为键,并从适当的值中获取listwidget entrys。 –