partially update default stylesheet
-
@Chris-Kawa said in partially update default stylesheet:
if platform style allows it, Qt can apply the qss style to just some elements without completely replacing the style, but that is not the case for border (often text color and such, but it differs on different platforms and is not portable).
Yes, but I never know which things can be changed without losing native styling and which cannot.
@JonB said in partially update default stylesheet:
Yes, but I never know which things can be changed without losing native styling and which cannot.
Because this depends on the underlying style which defines what can be changed and what not without breaking the rest.
-
@JonB said in partially update default stylesheet:
Yes, but I never know which things can be changed without losing native styling and which cannot.
Because this depends on the underlying style which defines what can be changed and what not without breaking the rest.
@Christian-Ehrlicher
I understand that, but as a dev I don't know which they are and which not. Just saying. At least that explains why sometimes you can make a change without breaking windowing style and sometimes you lose it. -
@tech032142 The widget uses your default platform style, which often draws the border and background with some OS provided images or brushes. When you call
setStyleSheet
you're changing the style of your widget to Qt's StylesheetStyle, so you're no longer using the platform style on any parts of the widget. In some cases, if platform style allows it, Qt can apply the qss style to just some elements without completely replacing the style, but that is not the case for border (often text color and such, but it differs on different platforms and is not portable).@Chris-Kawa It makes more sense now, i couldn't find this in the docs maybe I missed it ?
-
@Christian-Ehrlicher
I understand that, but as a dev I don't know which they are and which not. Just saying. At least that explains why sometimes you can make a change without breaking windowing style and sometimes you lose it.@JonB said in partially update default stylesheet:
At least that explains why sometimes you can make a change without breaking windowing style and sometimes you lose it.
Correct - it sometimes depends even on the underlying style.
-
I am on macOS Sonoma with Qt 6.9.3,
the first image is untouched no setStyleSheet calls, the other one is after setting styles
My question is :
setStyleSheet resets all style rules how do I preserve everything and just change the border color ?I also tried
"QGroupBox { border : 2px solid #CCCCCC}"
I am extending QGroupBox in that class by the way
@tech032142 said in partially update default stylesheet:
how do I preserve everything and just change the border color ?
This is general problem with stylesheets: You don't.
My personal experience (where it is most obvious) is the following: On Windows, by default, the "Ok" and "Cancel" buttons in a dialog have the same width. However, once you apply any stylesheet the "Ok" buttons is smaller than the "Cancel" button because the text is shorter. Applying any stylesheet at all will mess with several aspects of layouts and designs. Still, there is no better general options than to use stylesheets.
@JonB said in partially update default stylesheet:
Yes, but I never know which things can be changed without losing native styling and which cannot.
In a perfect world we could just adapt the palette instead of setting a stylesheet (at least for color). It's not gonna happen.
-
@tech032142 said in partially update default stylesheet:
how do I preserve everything and just change the border color ?
This is general problem with stylesheets: You don't.
My personal experience (where it is most obvious) is the following: On Windows, by default, the "Ok" and "Cancel" buttons in a dialog have the same width. However, once you apply any stylesheet the "Ok" buttons is smaller than the "Cancel" button because the text is shorter. Applying any stylesheet at all will mess with several aspects of layouts and designs. Still, there is no better general options than to use stylesheets.
@JonB said in partially update default stylesheet:
Yes, but I never know which things can be changed without losing native styling and which cannot.
In a perfect world we could just adapt the palette instead of setting a stylesheet (at least for color). It's not gonna happen.
@SimonSchroeder
I come from an HTML/CSS background. And therefore web pages/elements, not native windowing system ones. In the former I have no trouble adjusting any individual element style attribute, and if I need to I can read back existing attribute values e.g. from JavaScript. I realise this is not the same for Qt/QSS, which is trying to do some CSS against non-CSS-HTML windowing widgets, but that is where it gets "complicated/disappointing" to my experience/expectations. -
Hi,
Another option is to create a QProxyStyle to do the painting you want.
-
Hi,
Another option is to create a QProxyStyle to do the painting you want.
@SGaist said in partially update default stylesheet:
Another option is to create a QProxyStyle to do the painting you want.
I consider this quite complicated. And sometimes I just want the native look and just change a single thing (like the OP). I don't think it is reasonable that everybody reimplements the native look with a QProxyStyle. It would be more helpful if it was provided by Qt directly. At least with QtWidget I would rather be able to just set the palette to switch between light mode and dark mode and not write stylesheets for that.
-
There's no need to reimplement the whole style in the QProxyStyle. Just the things that you actually want to be modified.
See this nice article from KDAB on the matter. -
@SGaist said in partially update default stylesheet:
Another option is to create a QProxyStyle to do the painting you want.
I consider this quite complicated. And sometimes I just want the native look and just change a single thing (like the OP). I don't think it is reasonable that everybody reimplements the native look with a QProxyStyle. It would be more helpful if it was provided by Qt directly. At least with QtWidget I would rather be able to just set the palette to switch between light mode and dark mode and not write stylesheets for that.
@SimonSchroeder The problem is that "native style" means it uses native OS facilities to paint the control. Sometimes there's some level of customization provided by those, but more often than not there's none.
Take a widget background as an example - a Windows native style uses a call to DrawThemeBackgroundEx (among others). It just gives the OS a region and gets a painted image back. There's some degree of customization, e.g. you can pass a flag to omit a frame, but Qt code doesn't know what is drawn - a gradient, rounded corners, how thick the frame is etc.
The same executable with the same native style plugin will get something different on Windows Vista, 8, 10 or 11. That being said Qt can't "just let you change one thing". It gets all the things as a single bitmap, so you either take it or recreate the whole thing as a custom style (qss). Qt can't do it for you because it's dynamic and depends on the OS. If you decide to do it you take responsibility for it not looking native anymore. Trying to recreate native look with qss is futile, because native look can change at any given OS update. -
There's no need to reimplement the whole style in the QProxyStyle. Just the things that you actually want to be modified.
See this nice article from KDAB on the matter.@SGaist said in partially update default stylesheet:
There's no need to reimplement the whole style in the QProxyStyle. Just the things that you actually want to be modified.
See this nice article from KDAB on the matter.I didn't know about the article. However, I'm not entirely sure if the statement "Just the things that you actually want to be modified" is correct. It seems as if the article assumes that you don't want to use a native style anyway because the style should look the same on all platforms. Correct me if I'm wrong, but if I am using the Windows native style and I just want to change the border color of a
QPushButton
in my ownQStyle
-derived class, I only can try to change a single thing if I'm calling theQPushButton
paint method of the underlying style. This actually does not change anything. Instead, I need to draw my own button entirely to apply the changes because the native Windows style does not respect my wishes.Or I am wrong about this and there is actually some code that copies the native style (contrary to what @Chris-Kawa said). Then the big question is why I can't just change this by setting the QPalette instead...
-
@SGaist said in partially update default stylesheet:
There's no need to reimplement the whole style in the QProxyStyle. Just the things that you actually want to be modified.
See this nice article from KDAB on the matter.I didn't know about the article. However, I'm not entirely sure if the statement "Just the things that you actually want to be modified" is correct. It seems as if the article assumes that you don't want to use a native style anyway because the style should look the same on all platforms. Correct me if I'm wrong, but if I am using the Windows native style and I just want to change the border color of a
QPushButton
in my ownQStyle
-derived class, I only can try to change a single thing if I'm calling theQPushButton
paint method of the underlying style. This actually does not change anything. Instead, I need to draw my own button entirely to apply the changes because the native Windows style does not respect my wishes.Or I am wrong about this and there is actually some code that copies the native style (contrary to what @Chris-Kawa said). Then the big question is why I can't just change this by setting the QPalette instead...
@SimonSchroeder Native styles are implemented such that they follow the original platform style and are thus free to ignore modified palette values to stay coherent.
Thus, depending on what you want to change, you will need to check what the original style does underneath. I am not claiming that it's simple for everything, far from it.
Many times, it's a question of re-implementing drawPrimitive to either call the base class implementation with modified values or having your own implementation based out of the original class.