Can Background color be a variable in stylesheet
-
I am changing the appearance of my QComboBoxes with the following:
ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { background:rgb(241, 231, 218); }");
Is it possible to replace the { background:rgb(241, 231, 218);} with a variable name?
IE: QString bgrgb = "background:rgb(241, 231, 218);"
then ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { bgrgb }");
This does not work. the comboBox appears black in this implementation.
Thanks to all for any help
-
I am changing the appearance of my QComboBoxes with the following:
ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { background:rgb(241, 231, 218); }");
Is it possible to replace the { background:rgb(241, 231, 218);} with a variable name?
IE: QString bgrgb = "background:rgb(241, 231, 218);"
then ui->comboBox_0->setStyleSheet ("QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { bgrgb }");
This does not work. the comboBox appears black in this implementation.
Thanks to all for any help
Hi, @qt_emp
I guess you need to do it by yourself.
What about defining a const string and changing only the color ?
const QString ss = "QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;} QComboBox { background:%1 }"; QColor c(Qt::red); //Set background red ui->comboBox_0->setStyleSheet(ss.arg(c.name())); //... c = QColor(Qt::blue); //Set background blue ui->comboBox_0->setStyleSheet(ss.arg(c.name()));
I have used color name, instead of
r
,g
,b
, so only one value (%1
) is replaced in the string . -
The color names available in the palette are too limited. I needed other colors beyond the palette, thus the RGB implementation.
@qt_emp
That's not what I mean.
I usedQColor::name()
which returns string containing "#RRGGBB" in hexadecimal.
You can put any color you want in QColor.//Setting color using hexadecimal QColor c = "#AABBCC" ui->comboBox_0->setStyleSheet(ss.arg(c.name())); //Another example without using QColor ui->comboBox_0->setStyleSheet(ss.arg("#AABBCC"));