Qt, How do I change the text color of one item of a QComboBox? (C++)

Linoliumz picture Linoliumz · Jun 18, 2010 · Viewed 14.5k times · Source

I cannot figure out how to change the text color of one particular item of a QComboBox. I was able to change the Background color of an item:

comboBox->setItemData(i, Qt::green, Qt::BackgroundRole);

(Qt::ForegroundRole had no effect at all, Qt 4.6, Ubuntu 10.04)

and I was able to change the text color of all items with a stylesheet but I cannot figure out how to change the text color of one specified item.

Thanks for your Help!

Answer

Lefteris Banos picture Lefteris Banos · Jun 18, 2013

It's almost like you propose, but you have to change the role to Qt::TextColorRole.

comboBox->setItemData(0, QBrush(Qt::red), Qt::TextColorRole);