PDA

View Full Version : PyQt: QCombobox background text color not reflected in selected state



pyqt_dev2
9th February 2015, 09:55
Hi guys,

The following code works but doesn't do in full what exactly is needed:



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)

Now whenever I expand and view the dropdown, the names in colorNameList appear in red background. So far so good. However when I select one of these red-background names they do not show as red-background when left selected.

Any ideas how I can ensure that on selection red items appear red and non-red appears non-red.

Many thanks!

Rahul

wysota
10th February 2015, 08:29
When an entry is selected Qt applies a highlight color on it. You need to alter that as well.

pyqt_dev2
11th February 2015, 15:19
Hi wysota,

Thanks for your response. However can you advise on how this can be achieve in python. Please refer to the code snippet in my original post above.

Rahul

wysota
11th February 2015, 18:17
Your original snippet deals with a model while the highlight happens in the view through the delegate. Thus you need to either implement a custom delegate or set the highlight color in your view's palette to transparent.

pyqt_dev2
17th February 2015, 22:59
Hi wysota

Thanks for your response. I am new to PyQt; would it be possible for you to provide a code snippet to do so.

Many Thanks
Rahul

wysota
17th February 2015, 23:21
I am new to PyQt too so we're starting from the same point so you can provide that code snippet yourself as well. You have two hints -- custom delegate and changing the widget's palette.