I'm trying to make the highlight transparent for a QComboBox. The color of this QComboBox also changes based on the selected index. Here is my best solution so far:
switch(comboBox->currentIndex())
{
case 0:
comboBox->setStyleSheet("QWidget {color:black}");
break;
case 1:
comboBox->setStyleSheet("QWidget {background-color:red; color:white;}");
break;
case 2:
comboBox->setStyleSheet("QWidget {background-color:green; color:white;}");
break;
}
comboBox->setItemData(0, QColor(Qt::white), Qt::BackgroundRole);
comboBox->setItemData(0, QColor(Qt::black), Qt::ForegroundRole);
comboBox->setItemData(1, QColor(Qt::red), Qt::BackgroundRole);
comboBox->setItemData(1, QColor(Qt::white), Qt::ForegroundRole);
comboBox->setItemData(2, QColor(Qt::darkGreen), Qt::BackgroundRole);
comboBox->setItemData(2, QColor(Qt::white), Qt::ForegroundRole);
QPalette p = comboBox->palette();
p.setColor(QPalette::Highlight, Qt::transparent);
comboBox->setPalette(p);
p = comboBox->view()->palette();
p.setColor(QPalette::Highlight, Qt::transparent);
comboBox->view()->setPalette(p);
The problem is that whatever color the QComboBox currently is is what the highlight color will be when selecting an item in the popup. I would like each QComboBox item to stay the same color. The images show the problem I'm having.