@JonB said in QCombo selected item background colour:
@mrjj
His specification is
I can set the background colour of an item.
However, when this item is selected, the background changes some mid-gray. Once another item is selected, the background colour returns to the one I set.
I don't want to set the selected-item colour but want to display the pre-set colour even if it is selected.
He wants the background colour --- which he has already changed to his liking on non-selected items --- to stay as it was when an item is selected, rather than (at present) where it changes to some other color.
He has said nothing other than that about "highlighting", so e.g. if selecting changes the foreground color or boldness (I don't know if it does) we don't care, it's the background color which matters.
:)
Thanks for the reply, spot on!
[Could have gotten it done by now by just using stylesheet ;-) ]
Could have if i just knew WHAT to actually type in ... if you'd like to quickly type it out ...?
What I checked is:
https://doc-snapshots.qt.io/qt5-dev/stylesheet-examples.html#customizing-qcombobox
It has some haphazard examples, none seem to be relevant.
Back to the core documentation:
https://doc-snapshots.qt.io/qt5-dev/stylesheet-reference.html#list-of-properties
where it says about 1/4 way down the page:"
List of Properties
The table below lists all the properties supported by Qt Style Sheets. "
Here I found a relevant property: selection-background-color
Which says "The background of selected text or items."
Except that it does not. It temporarily changes the background color of the item the mouse is over.
Another relevant property is : background.
It says "Shorthand notation for setting the background. Equivalent to specifying background-color"
Except that not entirely. It does change the BG colour but it also changes the 'highlight' colour to the same. Highlight=the temporary change of the item's BG colour while the mouse is over it.
To overcome this problem, the highligt-BG-color must be reset too every time.
Highlight-BG-colour here means : selection-background-color. See code below.
I am still wondering, how can the 'selection-background-color' retrieved from the stylesheet-mess.
Reading:
https://doc.qt.io/qt-5/qt.html
about 2/3 down says: "Roles describing appearance and meta data (with associated types):"
In the following list i couldn't find any relevant role to get the current 'selection-background-color'.
Also, so far I couldn't find anything on google.
If you feel that i am totally lost and just about randomly shooting in the dark: you are not wrong. That is exactly what i feel.
If someone could give a more intelligent code, it would be appreciated.
This is the code that almost works:
(For clarity's sake, irrelevant code is omitted)
some-function()
{
QStringList comboitems;
comboitems << "Item 1" << "Item 2" << "Item 3";
ui->comboBox->addItems(comboitems);
ui->comboBox->setItemData(0,QColor(Qt::blue),Qt::BackgroundRole);
ui->comboBox->setItemData(1,QColor(Qt::red),Qt::BackgroundRole);
ui->comboBox->setItemData(2,QColor(Qt::yellow),Qt::BackgroundRole);
}
void MainWindow::on_comboBox_activated(int index)
{
/*one can't simply currentStyle = combo.getStylesheet because these are cascading and the object only knows
about the result. It is unable to return the source. It can return the stylesheet if you set one. Welcome to the beauty of the styleshit.
*/
//get the current BG colour
QVariant var = (ui->comboBox->itemData(index, Qt::BackgroundRole));
//convert to QColor
QColor mycolor = var.value<QColor>();
//there is no built-in function to convert QColor to a string ... but the styleshit needs a string
int red = mycolor.red();
int green = mycolor.green();
int blue = mycolor.blue();
QString strRGB = "rgb(" + QString::number(red) + "," + QString::number(green)+ "," + QString::number(blue) + ")";
qDebug() << "strRGB: " << strRGB;
//the current selection-beckground-color shoud be retrieved but i have no idea how to do this. Thus it is hard-coded.
//... and FINALLY!
//Set the BG colour AND reset the selection-highlight colour
ui->comboBox->setStyleSheet("background:" + strRGB + "; selection-background-color: yellow;");
}