Unsolved QCSS precedence versus "in-line code"
-
Try adding the button to the layout first, then applying your explicit changes. I suppose what happens now is that you set up your custom widget, and then when you add it to layout, the layout is pushing it's stylesheet to that widget.
-
@JonB said in QCSS precedence versus "in-line code":
So, in Qt, I guess I need to regard anything set via a style sheet, at any level, as overriding any corresponding thing the code may have set on the widget explicitly. Is that right?
No, that is wrong. QSS works only for the widget you set it on + all children. It does not go up in the parent-child tree.
-
@sierdzio said in QCSS precedence versus "in-line code":
@JonB said in QCSS precedence versus "in-line code":
So, in Qt, I guess I need to regard anything set via a style sheet, at any level, as overriding any corresponding thing the code may have set on the widget explicitly. Is that right?
No, that is wrong. QSS works only for the widget you set it on + all children. It does not go up in the parent-child tree.
For this one, I think you're misunderstanding me. I know that inheritance is only downward, not upward (remember, I'm familiar with CSS!). I didn't imply otherwise. I meant: no matter where I do set CSS --- e.g. directly on a widget, or on any level of parentage --- that is going to override any explicit, say,
setMinimumHeight()
calls the code might make directly on the widget, of which there are probably many to track down in the code. And it will apply to any call, not justsetMinimumHeight()
..... -
@sierdzio said in QCSS precedence versus "in-line code":
Try adding the button to the layout first, then applying your explicit changes. I suppose what happens now is that you set up your custom widget, and then when you add it to layout, the layout is pushing it's stylesheet to that widget.
Nope! Done that (moved
self.centralLayout.addWidget(self.btn2)
up to immediately belowself.btn2 = QtWidgets.QPushButton("Button #2")
), behaviour is exactly the same, stylesheet overrides "in-line code" ... ? -
@JonB said in QCSS precedence versus "in-line code":
Nope! Done that (moved self.centralLayout.addWidget(self.btn2) up to immediately below self.btn2 = QtWidgets.QPushButton("Button #2")), behaviour is exactly the same, stylesheet overrides "in-line code" ... ?
Looks like a bug to me, then.
-
@sierdzio
Well, I can't see it's a "bug", as it must be a thorough-going behaviour principle. That's why I illustrated both with a size and a color.Is someone able to confirm what the "principle" is here, please?
-
@JonB said in QCSS precedence versus "in-line code":
Well, I can't see it's a "bug", as it must be a thorough-going behaviour principle.
If it is not mentioned in the documentation, it is a bug. Besides, the intuition is that when you change something explicitly, it should not be overridden by default global setting, IMO. So it feels buggy to me (esp. since I don't remember it working this way previously).
-
@sierdzio
I take your point. However it's a pretty fundamental design one way or the other, and I have refactor all code in that light now, I cannot wait till Qt might change its mind in the future. So my immediate problem is to figure out what I might need to look for in my code now which may need changing as a consequence. -
Hi,
From the style sheet documentation. It’s coherent. Style sheets guarantee that what you set is what you get and only what it doesn’t handle, it forwards to the OS style.
So depending on your needs, you might want to consider implementing your own style like Qt Creator does.
-
@SGaist
Thank you as ever for your response.I have (re-)read the link, and I did not get from it that an explicit, say,
QWidget::setMaximumWidth()
would be overridden by a generic stylesheetmax-width:
. But I am not as familiar with Qt as you are.So, whether I like it or not, if an application stylesheet has
QPushButon { max-width: 100px; }
, and that suits 90% of the buttons in the app, but dotted over the code are the occasionalbtn.setMaximumWidth(XXX)
s with different numbers and not suitable for common stylesheet rules, they will have no effect. They would need to be changed tobtn.setStyleSheet("max-width: XXXpx;")
instead if I want to be sure they come into effect.Once stylesheets are in use, you must use
setStyleSheet()
and not the dedicated Qt properties to be sure to override. Right? -
Yes that's right. However, like I wrote before depending on your needs, a custom QStyle might be more suitable.
-
@SGaist
OK, thanks for confirming.What link should I read up for "a custom QStyle" and "you might want to consider implementing your own style like Qt Creator does", as I don't know about this?