I would like to make a combo box that selects some contents associated with colors. I want the background of the contents to show the color. I have achieved this:
QList<QString> names;
QList<QColor> bgColors;
QList<QColor> fgColors;
QComboBox* colorComboBox = new QComboBox();
for(int i = 0; i < names.size(); ++i)
{
colorComboBox->addItem(names.at(i), bgColors.at(i));
const QModelIndex idx = colorComboBox->model()->index(i, 0);
colorComboBox->model()->setData(idx, bgColors.at(i), Qt::BackgroundColorRole);
colorComboBox->model()->setData(idx, fgColors.at(i), Qt::ForegroundRole);
}
The combo box shows the text I want, with the background color I want (not as delicate as the ColorEditorFactory example, which only shows a small rectangle next to the text, but this is how I wanted it).
What I need:
Once a row/color is selected, I would like the combo box to show the color. As is now, the combo box when closed shows the text but not the color.
How can I change the color of the combo box header ? (I mam calling it header but it may have a different name, not sure - the part that shows above the table used for selection and when the combo box is closed)
Edit: I tried to set a stylesheet in a slot on currentIndexChanged
:
setStyleSheet("QComboBox { color: " + fgColor +
"; background-color: " + bgColor + "; }");
Result: it changed the entire combo box in that color, forgetting the initial colors.
setStyleSheet("QComboBox:!on { color: " + fgColor +
"; background-color: " + bgColor + "; }");
Result: it changed the color nicely when not selected - but the highlight and the header are gray and hard to read, I wish I could change it also. And when I hover, the entire combo color changes to the last one I set.
The answer may be in stylesheets - if I can figure out what property applies to the header.