0

Merry Christmas guys!

I am new to PyQt4 programming and using Qt Designer for most of UI development. However I have a specific requirement to populate a QComboBox through Python code. More so I want to change text formatting (Bold, Red Background color) for some of the entries.

Essentially the combobox entries should appear as follows:


John
Tom
Henry
Michelle
Ashish
Jo-Huang
....

In terms of code I want to do something like this:

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)

The following code failed:

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)

Error msg:

TypeError: arguments did not match any overloaded call:
QComboBox.addItem(QString, QVariant userData=QVariant()): argument 1 has unexpected type 'QStandardItem'
QComboBox.addItem(QIcon, QString, QVariant userData=QVariant()): argument 1 has unexpected type 'QStandardItem'

The following code worked :):

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)


Many Thanks
Rahul

Rahul
  • 755
  • 4
  • 8
  • 16
  • Thanks Reticality for your response. However that doesn't work. Getting following error:
    TypeError: arguments did not match any overloaded call: QComboBox.addItem(QString, QVariant userData=QVariant()): argument 1 has unexpected type 'QStandardItem' QComboBox.addItem(QIcon, QString, QVariant userData=QVariant()): argument 1 has unexpected type 'QStandardItem'
    – Rahul Dec 25 '14 at 22:14
  • What exactly is your question? You have code that works. The error with the code that doesn't work is because the method `QComboBox.addItem()` is not expecting to be passed an instance of `QStandardItem` (as the error message clearly states). `QStandardItem`s should be appended/inserted into the model for the combobox (which is what you do in the code that works). Am I missing something? – three_pineapples Dec 26 '14 at 04:28
  • 1
    @Rahul, you probably meant to notify that you found the solution, but posted that in your own question. If you found out a solution, please post it as an *answer* here. – kartikg3 Dec 26 '14 at 21:55

1 Answers1

0

The following code worked :):

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)

Cheers
Rahul

Rahul
  • 755
  • 4
  • 8
  • 16