PDA

View Full Version : How to get a stylesheet property?



P@u1
19th September 2011, 17:32
Hi,

I must know the background color of one of my widgets to set other components of this widget to the right color.
So far it looked like this:

m_blackCurve->setPen(QPen(m_Plot->palette().background().color()));

Now I'm using stylesheets so thhe palette().background() mehods don't work anymore (give wrong results).

How can I access the background color, which was set in the stylesheet, or is there maybe a better way to fix this?

Lykurg
19th September 2011, 18:23
or is there maybe a better way to fix this?Jeah, don't use style sheets! Only use palette. Otherwise you have to query the style sheet (QWidget::styleSheet()) and do some regular expressions to get the color out of the string.

wysota
20th September 2011, 02:34
And since stylesheets are inherited, you might have to do that multiple times.

syclopse
20th September 2011, 08:12
use any paint like application and get the color combination,
and then apply that combination to that widget's background color in stylesheet.

P@u1
20th September 2011, 09:40
I don't get the background color of the window changed with QPalette...

with stylesheets I used this:

m_centralWidget->setStyleSheet("border: 1px solid grey; background: black; color: green");
And it worked very well.

Now I tried this with QPalette (I changed so many colors only because it didn't work):

QPalette palette = m_centralWidget->palette();
palette.setColor(QPalette::Window, QColor(0,0,0));
palette.setColor(QPalette::Foreground, QColor(0, 255,0));
palette.setColor(QPalette::Base, QColor(0, 0 ,0));
palette.setColor(QPalette::AlternateBase, QColor(0, 0 ,0));
palette.setColor(QPalette::ToolTipBase, QColor(0, 0 ,0));
palette.setColor(QPalette::ToolTipText, QColor(0, 0 ,0));
palette.setColor(QPalette::Text, QColor(0, 0 ,0));
palette.setColor(QPalette::Button, QColor(0, 0 ,0));
palette.setColor(QPalette::ButtonText, QColor(0, 0 ,0));
palette.setColor(QPalette::BrightText, QColor(0, 0 ,0));
m_centralWidget->setPalette(palette);

But the background of the window remains in the normal color (some kind of grey).

Why it does not work?

wysota
20th September 2011, 10:01
If you're running modern Windows then it doesn't allow to change background color of most widgets. You need to switch the widget style to something that allows it first using QWidget::setStyle().

P@u1
20th September 2011, 10:03
Hm with stylesheets it was much easier...
Is there no good way to extract the color information from the stylesheet?

Maybe I will create some regex for it..

Uwe
20th September 2011, 10:15
Jeah, don't use style sheets! Only use palette. Otherwise you have to query the style sheet (QWidget::styleSheet()) and do some regular expressions to get the color out of the string.
Yes getters are completely missing in the design of style sheets. In Qwt ( the plot canvas ) I paint a styled background to a dummy paint device for reverse engineering the background ( I need to know the border radius ).

Note, that using style sheets also has an impact on the performance, because Qt repaints complete widgets ( instead of update regions only ) in certain situations - depending on the Qt::WA_StyledBackground flag. This can be a serious issue - f.e. on Qt/Embedded with a slow frame buffer.

IMO the whole style sheet concept looks easy in the beginning, but wasn't never thought to its end.

Uwe

P@u1
20th September 2011, 10:20
apropos qwt.
What I'm trying to do is setting the color of the plot curve colors according to the style sheet of the parent widgets.

Lykurg
20th September 2011, 10:23
have you tried
m_centralWidget->setAutoFillBackground(true);?

Uwe
20th September 2011, 10:35
apropos qwt.
What I'm trying to do is setting the color of the plot curve colors according to the style sheet of the parent widgets.
I can imagine to derive a color from the palette, but as you don't have any information in your application about the current stylesheet ...

Uwe

wysota
20th September 2011, 15:19
IMO the whole style sheet concept looks easy in the beginning, but wasn't never thought to its end.
Stylesheets were meant for designers, not programmers. If we (programmers) try to abuse them (for example by trying to know some colors or sth), it usually ends badly.

Uwe
20th September 2011, 16:50
Stylesheets were meant for designers, not programmers. If we (programmers) try to abuse them (for example by trying to know some colors or sth), it usually ends badly.
Well, think about a background with a border radius - this is something you can set for all type of widgets. Further more: this is something you can do with style sheets only ( missing features in the C++ API are one of the main reason for using style sheets ). So as author of a 3rd party library you need to take care of style sheets - or write in the documentation, that your widgets fail in combination with them.

Most Qt widgets can simply increase the contents margins, so that the content is something rectangular again. But as soon as you can't ( or don't want to ) have these margins ( f.e. when displaying a QImage on a label ) you have the following problems:


How to clip the image against the rounded border
The border is part of the background and so painted before the content of the widget.
But as you need antialiasing here the border needs to be painted independent of the background on top of the content.

Last but not least there is absolutely no concept for style sheets and 3rd party widgets - what might have to do with the design decision to implement style sheets as a style proxy. I have no chance to make the Qwt widgets styleable by style sheets beside the couple of attributes the widget derives from the base class - where I even don't have any clean interface to know them.

Uwe

wysota
20th September 2011, 17:04
Well, think about a background with a border radius - this is something you can set for all type of widgets. Further more: this is something you can do with style sheets only ( missing features in the C++ API are one of the main reason for using style sheets ). So as author of a 3rd party library you need to take care of style sheets - or write in the documentation, that your widgets fail in combination with them.
I'm not saying we shouldn't use stylesheets or try to be compliant with them. But in the end it is the stylesheet designer who should make sure the style is complete and looks ok. Stylesheets are meant to be placed "on top" of a widget, not the other way round. It's a cool way to have a red pushbutton on Windows but one has to be aware stylesheets are simply not fit to do some things. We should try to be as compliant with them as possible (as you have written) but not be paranoid.

Uwe
20th September 2011, 17:37
We should try to be as compliant with them as possible (as you have written) but not be paranoid.
I agree, but often widgets could be more compliant easily - if they would know with what.

Uwe

wysota
20th September 2011, 21:15
Yes, that's true. I would at least like to have a couple of paragraphs in the docs on how to make custom widgets as compliant with stylesheets as possible. Unfortunately Qt docs are not as good as they used to be when David Boddie was around...