Howto set a single item in stylesheet
-
I have a quite complex stylesheet
@font: 72pt "Arial";
color: rgb(0, 0, 209);
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(212, 255, 166), stop:1 rgba(177, 255, 255));
border-radius: 25px;
border-width: 8px;
padding: 6px;
border-style: outset;
border-color: rgb(0, 0, 127);
@and I'd like to set the "color" property to different colors at runtime.
The
@int actualcolor= 1;
setProperty("whichone", actualcolor);
unpolish();
polish();
@...and in stylesheet use conditionals on the "whichone" property:@QLineEdit[whichone=1] {color: black;}
QLineEdit[whichone=2] {color: red;}
@approach can't be used if more than a couple different colors to set.
I must cut and replace the "color: rgb(0, 0, 209);" part of the stylesheet with string operations:
@
QString ss= widget->styleSheet();
ss.replace_somehow();
widget->setStyleSheet(ss);
@Has anyone seen a function like
@setStyleSheetProperty("color","rgb(250, 175,143)");@?
(Qt's internal stylesheet-string-interpreter probably will process the string like that.) -
Yes, this is working now:
@QString basestyle= "font: 72pt "Arial"; border-radius: 25px; border-width: 8px; padding: 6px; border-style: outset; ";
if (value < 1) {
ui->lbPozicio->setStyleSheet(basestyle);
return;
}
else if (value < 5) {
ui->lbPozicio->setStyleSheet(basestyle + "border-color: rgb(0, 0, 127); background-color: rgba(212, 255, 166); color: rgb(0, 0, 209);");
return;
}
else if (value < 10) {
ui->lbPozicio->setStyleSheet(basestyle + "border-color: rgb(85, 0, 0); color: rgb(130, 0, 0); background-color: rgba(255, 212, 166);");
return;
}
...
@
It is really a rare case, that I don't know the "base style" for the widget, so I have to take off from the actual string, cut the old property value, and replace with another one.
Maybe this
@setStyleSheetProperty(QString propertyname, QString newvalue)@
function should go to the 4.8 wishlist only. -
This is a "big display" QLabel, most of its properties are different from those of its parent.
(Bigger font, thicker border with higher radius etc.)
My first try was simply@ui->lbPozicio->setStyleSheet("border-color: rgb(85, 0, 0); color: rgb(130, 0, 0); background-color: rgba(255, 212, 166);");
@
but properties missing from this expression got their (inappropriate) values from QLabel's parent.It would be much more pleasant behaviour for me to have a
setStyleSheetProperty() and
clearStyleSheetProperty()If I have plenty of time, I am going to look after the re-evaluation of stylesheet-chain in the Qt source. I think there must be a point, where these functions can be added quite easily.
-
Shell out plenty of time then, because I for one thought it was quite a complex piece of code...
It is an inherent property of cascading style sheets that styles set on a parent item cascade down to the child items. Sometimes, that can be annoying, but it can usually be solved by being more specific in defining the selectors for the style in the first place.