Results 1 to 16 of 16

Thread: How to get a stylesheet property?

  1. #1
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default How to get a stylesheet property?

    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:
    Qt Code:
    1. m_blackCurve->setPen(QPen(m_Plot->palette().background().color()));
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by P@u1 View Post
    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get a stylesheet property?

    And since stylesheets are inherited, you might have to do that multiple times.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2011
    Posts
    39
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a stylesheet property?

    use any paint like application and get the color combination,
    and then apply that combination to that widget's background color in stylesheet.

  5. #5
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: How to get a stylesheet property?

    I don't get the background color of the window changed with QPalette...

    with stylesheets I used this:
    Qt Code:
    1. m_centralWidget->setStyleSheet("border: 1px solid grey; background: black; color: green");
    To copy to clipboard, switch view to plain text mode 
    And it worked very well.

    Now I tried this with QPalette (I changed so many colors only because it didn't work):
    Qt Code:
    1. QPalette palette = m_centralWidget->palette();
    2. palette.setColor(QPalette::Window, QColor(0,0,0));
    3. palette.setColor(QPalette::Foreground, QColor(0, 255,0));
    4. palette.setColor(QPalette::Base, QColor(0, 0 ,0));
    5. palette.setColor(QPalette::AlternateBase, QColor(0, 0 ,0));
    6. palette.setColor(QPalette::ToolTipBase, QColor(0, 0 ,0));
    7. palette.setColor(QPalette::ToolTipText, QColor(0, 0 ,0));
    8. palette.setColor(QPalette::Text, QColor(0, 0 ,0));
    9. palette.setColor(QPalette::Button, QColor(0, 0 ,0));
    10. palette.setColor(QPalette::ButtonText, QColor(0, 0 ,0));
    11. palette.setColor(QPalette::BrightText, QColor(0, 0 ,0));
    12. m_centralWidget->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

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

    Why it does not work?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get a stylesheet property?

    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: How to get a stylesheet property?

    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..

  8. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by Lykurg View Post
    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

  9. #9
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: How to get a stylesheet property?

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to get a stylesheet property?

    have you tried
    Qt Code:
    1. m_centralWidget->setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 
    ?

  11. #11
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by P@u1 View Post
    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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by Uwe View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by wysota View Post
    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

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by Uwe View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get a stylesheet property?

    Quote Originally Posted by wysota View Post
    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

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get a stylesheet property?

    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...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. looking for SortRole property example
    By umen in forum Qt Programming
    Replies: 0
    Last Post: 4th July 2011, 14:11
  2. Virtual property ?
    By gorsanmo in forum Newbie
    Replies: 2
    Last Post: 10th May 2011, 08:23
  3. Change Stylesheet Using Dynamic Property
    By stefanadelbert in forum Qt Programming
    Replies: 4
    Last Post: 26th August 2010, 08:48
  4. Replies: 1
    Last Post: 18th February 2009, 09:34
  5. enum property
    By illuzioner in forum Qt Tools
    Replies: 10
    Last Post: 22nd August 2006, 22:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.