我是PyQt4编程的新手,并且在大多数UI开发中使用Qt Designer。但是我有一个特定的要求,通过Python代码填充QComboBox。更多,所以我想更改某些条目的文本格式(粗体,红色背景颜色)。
基本上,组合框条目应如下所示:
约翰
汤姆
亨利
的米歇尔
阿希什
乔·黄
....
在代码方面我想做这样的事情:
nameList = ('John','Tom','Henry','Michelle','Ashish','Jo-Huang', ...)
colorNameList = ('Michelle','Jennifer','Claudia','JimSung', ...)
callBackObj.NameComboBox.clear()
callBackObj.NameComboBox.addItem(QString('Account Names'))
for name in nameList:
if name in colorNameList:
callBackObj.NameComboBox.addItem(name) #Make this entry bold and red background
else:
callBackObj.NameComboBox.addItem(name)
以下代码失败:
nameList = ('John','Tom','Henry','Michelle','Ashish','Jo-Huang', ...)
colorNameList = ('Michelle','Jennifer','Claudia','JimSung', ...)
callBackObj.NameComboBox.clear()
callBackObj.NameComboBox.addItem(QString('Account Names'))
for name in nameList:
item = PyQt4.QtGui.QStandardItem(str(account))
if name in colorNameList:
item.setBackground(PyQt4.QtGui.QColor('red'))
callBackObj.NameComboBox.addItem(item)
错误消息:
TypeError:参数与任何重载调用都不匹配:
QComboBox.addItem(QString,QVariant userData = QVariant()):参数1具有意外类型'QStandardItem'
QComboBox.addItem(QIcon,QString,QVariant userData = QVariant()):参数1具有意外类型'QStandardItem'
以下代码有效:):
nameList = ('John','Tom','Henry','Michelle','Ashish','Jo-Huang', ...)
colorNameList = ('Michelle','Jennifer','Claudia','JimSung', ...)
callBackObj.NameComboBox.clear()
callBackObj.NameComboBox.addItem(QString('Account Names'))
model = callBackObj.NameComboBox.model()
for name in nameList:
item = PyQt4.QtGui.QStandardItem(str(account))
if name in colorNameList:
item.setBackground(PyQt4.QtGui.QColor('red'))
model.appedRow(item)
非常感谢
的Rahul
答案 0 :(得分:0)
以下代码有效:):
nameList = ('John','Tom','Henry','Michelle','Ashish','Jo-Huang', ...)
colorNameList = ('Michelle','Jennifer','Claudia','JimSung', ...)
callBackObj.NameComboBox.clear()
callBackObj.NameComboBox.addItem(QString('Account Names'))
model = callBackObj.NameComboBox.model()
for name in nameList:
item = PyQt4.QtGui.QStandardItem(str(account))
if name in colorNameList:
item.setBackground(PyQt4.QtGui.QColor('red'))
model.appedRow(item)
干杯
的Rahul