setStyleSheet sometimes not working
-
I have a problem using setStyleSheet() in a widget. I would like to adjust the font size from a stylesheet file. In the two examples, the only difference is the argument for setStyleSheet().
This worksData::Data(DataBase::Signal index, QString size, QWidget* parent, uint16_t subindex) : Button(parent), index(index), subindex(subindex), alive(false) { setAttribute(Qt::WA_StyledBackground); setAlignment(Qt::AlignCenter); setObjectName(size); setStyleSheet("font-size: 26pt;"); QHBoxLayout* layout = new QHBoxLayout(this); icon = new QLabel(this); data = new QLabel(this); unit = new QLabel(this); layout->addWidget(icon); layout->addWidget(data); layout->addWidget(unit); data->setText("0"); }
but
Data::Data(DataBase::Signal index, QString size, QWidget* parent, uint16_t subindex) : Button(parent), index(index), subindex(subindex), alive(false) { setAttribute(Qt::WA_StyledBackground); setAlignment(Qt::AlignCenter); setObjectName(size); setStyleSheet("Data#SMALL { font-size: 26pt; }"); QHBoxLayout* layout = new QHBoxLayout(this); icon = new QLabel(this); data = new QLabel(this); unit = new QLabel(this); layout->addWidget(icon); layout->addWidget(data); layout->addWidget(unit); data->setText("0"); }
Does not work.
-
@Jakob-Clausen
Have you verifiedqDebug() << size;
for us? We do not know what might or might not be in that..... -
Yes. I have.
It can be either "SMALL", "MEDIUM" or "LARGE" and all they do is change the font size.
There is no check to see if the objectname is one of the three.
But in this example I have checked it manually as you suggest, and it is "SMALL". -
@Jakob-Clausen
Try with selector of justData
and/or#SMALL
in place ofData#SMALL
: any difference? What about with just*
? -
Thats interesting.
setStyleSheet("Data { font-size: 26pt; }");
or
setStyleSheet("#SMALL { font-size: 26pt; }");
does not work.
Neither doessetStyleSheet("{ font-size: 26pt; }");
But as you suggest
setStyleSheet("* { font-size: 26pt; }");
does.
Does that make any sense? -
@Jakob-Clausen
*
is the selector for "anything". This implies to me that none ofData#SMALL
,Data
or#SMALL
correctly address your widget instance, or maybe the right "bit" of it, but I do not know why. What is the ultimate base Qt type of yourData
class? -
It is derived from QLabel.
-
@Jakob-Clausen
setStyleSheet("QLabel { font-size: 26pt; }");
? -
That works! :-)
What does that mean? -
@Jakob-Clausen
One outside thought is: does yourData
class use theQ_OBJECT
macro in its definition? That is usually required for signals, I have no idea whether that just might be required for stylesheet-widget-inheritance to work, worth checking.Otherwise, produce the absolute minimum standalone program which creates a
QLabel
and showsetStyleSheet("QLabel { font-size: 26pt; }")
working, together with the minimum for a derivedMyLabel
and show thatsetStyleSheet("MyLabel { font-size: 26pt; }")
does not work.P.S.
YourData
class does not reside in a namespace, does it??P.P.S.
I don't know whether this applies to deriving fromQLabel
, but for deriving fromQWidget
read https://forum.qt.io/topic/55983/inheritance-from-qwidget-stylesheet-problem & https://stackoverflow.com/questions/7276330/qt-stylesheet-for-custom-widget. But I'm really thinking/pretty sure that applies toQWidget
only.... -
I use Q_OBJECT and it is not in a namespace.
I have also tried to reimplement the event callback without any luck.
Now I will try to create a minimal standalone example. -
Ok. I have not yet created the absolute minimum standalone program, but I have something else.
If I dosetStyleSheet("Data QLabel { font-size: 26pt; }")
then it works. :-)
But why? Isn't the stylesheet supposed to be passed along to children automatically? (I guess not).So Data#SMALL only applies to Data-objects with the name SMALL and not its descendants and Data QLabel applies to all QLabel descendants of Data.
Is what I just wrote correct? -
@Jakob-Clausen
But you said earlier thatData
is derived fromQLabel
(not e.g. that it contains aQLabel
)? That's why I asked? -
Ah.
Sorry about that.
It does both. -
But many thanks for your effort.
I appreciate it. -
@Jakob-Clausen said in setStyleSheet sometimes not working:
It does both.
It is a
QLabel
, and it also contains (another)QLabel
?P.S.
As a tip/suggestion, don't pickData
as a class name, and certainly not for some GUI element like labels. -
Yes.
In a layout.
Is that bad? -
@Jakob-Clausen said in setStyleSheet sometimes not working:
Is that bad?
[Nested
QLabel
s.] I don't know, I've never done it!