Reputation: 1009
Hi I'm trying to set different properties in separate lines. Outcome is it is only taking last properly rather than all properties.
Ex:
QLabel *lb = new QLabel();
lb->setText("Hello");
lb->setStyleSheet("border: 20px solid grey");
lb->setStyleSheet("QLabel {background-color : black;}");
lb->setStyleSheet("QLabel {color : white;}");
lb->show();
This example will only set text color white and other properties are not getting updated.
I know one solution to set all the properties one goes like,
lb->setStyleSheet("QLabel { background-color : black; color : white; border: 20px solid grey}");
But I want to set them separately and using setStyleSheet. Thanks in advance
Upvotes: 0
Views: 63
Reputation: 3974
Perhaps something like this would be more appealing instead?
lb->setStyleSheet(
"border: 20px solid grey;"
"QLabel {"
"background-color : black;"
"color : white;"
"}"
);
It's basically the same as putting them all in one string, but it is broken into several lines visually.
PS. Not sure if that first line is actual valid css code as it isn't in a selector box... but it may be something part of Qt instead so I left it.
EDIT:
In the case you need to add each line in a different place in code, you could get the current stylesheet and then add to it
lb->setStyleSheet(lb->styleSheet() + "border: 20px solid grey;");
lb->setStyleSheet(lb->styleSheet() + "QLabel {background-color: black}");
lb->setStyleSheet(lb->styleSheet() + "QLabel {color: white}");
Upvotes: 1
Reputation: 4344
Every time you call setStyleSheet
it overrides the previous value.
What you can do is to make a method which collects all the styles into one line.
QString collectAllStyles() const
{
return QString("QLabel { background-color : %1; color : %2; border: %3}").arg(backgroundStyleText()).arg(colorStyleText()).arg(borderStyleText());
}
When one of the colors is changed just make this call:
lb->setStyleSheet(collectAllStyles());
Upvotes: 1