Reputation:
Having trouble understanding how to programmatically set a stylesheet to be applied on several (or even all) widgets in Qt Creator 4.1 (Qt 5.7) with C++.
For example, say I have 3 progress bar widgets; I have explicitly set each with the same stylesheet with css like so:
ui->c1->setStyleSheet("QProgressBar {"
"background-color: #74c8ff;"
"color: #0a9dff;"
"border-style: outset;"
"border-width: 2px;"
"border-color: #74c8ff;"
"border-radius: 7px;"
"text-align: left; }"
"QProgressBar::chunk {"
"background-color: #010327; }");
ui->c2->setStyleSheet("QProgressBar {"
"background-color: #74c8ff;"
"color: #0a9dff;"
"border-style: outset;"
"border-width: 2px;"
"border-color: #74c8ff;"
"border-radius: 7px;"
"text-align: left; }"
"QProgressBar::chunk {"
"background-color: #010327; }");
ui->c3->setStyleSheet("QProgressBar {"
"background-color: #74c8ff;"
"color: #0a9dff;"
"border-style: outset;"
"border-width: 2px;"
"border-color: #74c8ff;"
"border-radius: 7px;"
"text-align: left; }"
"QProgressBar::chunk {"
"background-color: #010327; }");
I would like to use the Qt Stylesheets to create a stylesheet for my widgets and avoid all this code suplication.
After reading the docs, the syntax would be like this :
QProgressBar
{
background: #74c8ff;
color: #0a9dff;
border-style: outset;
border-width: 2px;
border-color: #74c8ff;
border-radius: 7px;
text-align: left;
}
QProgressBar::chunk
{
background-color: #010327;
}
but, apparently you cannot copy the parameter names as used in a setStylesheet
with css directly to a syntax like the one above.
I also tried something like this
QProgressBar.setStyleSheet("QProgressBar {"
"background-color: #74c8ff;"
"color: #0a9dff;"
"border-style: outset;"
"border-width: 2px;"
"border-color: #74c8ff;"
"border-radius: 7px;"
"text-align: left; }"
"QProgressBar::chunk {"
"background-color: #010327; }");
None of the above worked (undeclared identifiers and syntax errors).
Could someone provide an example of how you can achieve defining a single stylesheet with the above parameters to be applied for all widgets? If you could point to a resource (I could not find anything!) that explains the available parameters to achive my goal, that would be as good).
Finally, if such a stylesheet is created for all progress bars, how can I exclude one progress bar (or any widget for that matter) from having that stylesheet and have instead a different one?
Upvotes: 1
Views: 12953
Reputation: 656
Use qApp->setStyleSheet
instead of QProgressBar.setStyleSheet
to set stylesheet for all widgets in your application. More examples here: http://doc.qt.io/qt-5/stylesheet-examples.html
Upvotes: 3