How can I style a span that is part of a widget text (e.g. a QLabel) via global style sheet?
E.g. in below example, both foo and bar should be red.
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
class
some_label : public QLabel
{
Q_OBJECT
public:
some_label(QString text = "") : QLabel(NULL) {
setText(text);
show();
};
};
#include "main.moc"
static const char *css =
"some_label { color : blue; background : black; }"
"span.some_class { color : red; }";
int
main(int argc, char **argv)
{
QApplication a(argc, argv);
a.setStyleSheet(css);
some_label label("before "
"<span style=\"color:red;\">foo</span> " /* works */
"<span class=\"some_class\">bar</span> " /* fails */
"after");
return a.exec();
}
Qmake (Qt 5.1.1) project file:
QT += widgets
SOURCES += main.cpp
I would really appreciate a solution that avoids hardcoding the style as I did
with the foo span.
The goal is for the application look to be determined entirely by a
user-supplied style sheet (represented by css
in the example).
At the moment I use a workaround involving separate labels for each element
that is colored and it is a nightmare to maintain.
I have consulted a few online sources as well as Blanchette/Summerfield chapter 19 but those are primarily concerned with styling whole widgets.