Results 1 to 12 of 12

Thread: using setStyleSheet

  1. #1
    Join Date
    Jan 2006
    Location
    Hannover, Germany
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default using setStyleSheet

    Hi all,

    I change the background color of my QPushButtons dynamically using Style Sheets:
    Qt Code:
    1. button->setPalette(QPalette(color));
    2. #ifdef Q_OS_WIN
    3. button->setStyleSheet( QString("background-color: %1;"
    4. "border: 2px solid #8f8f91; "
    5. "border-radius: 6px;"
    6. "min-width: 80px;" ).arg( color.name() ) );
    7. #endif
    To copy to clipboard, switch view to plain text mode 
    If I want to use the same approach to get another color for the pressed-state of a button, what is the right syntax to achieve it (and without having an extra text file mybutton.css)?

    Greets,
    Weilor

  2. #2
    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: using setStyleSheet

    * { ...}
    :pressed { ... }

    Better yet apply that on the parent or on the application object and use class or id selectors to select the object you want.

  3. #3
    Join Date
    Jan 2006
    Location
    Hannover, Germany
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using setStyleSheet

    Thanks wysota,

    Better yet apply that on the parent or on the application object and use class or id selectors to select the object you want.
    And why?

    Greets,
    Weilor

  4. #4
    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: using setStyleSheet

    Because you'll have more control. Doubts like this one wouldn't even occur, because you'd be calling the object by name.

  5. #5
    Join Date
    Sep 2007
    Posts
    18
    Thanks
    3

    Default Re: using setStyleSheet

    Hi wysota!

    If I want to change only the font color of a widget by using stylesheets,

    say i write it like this

    this->setStyleSheet("color: red");

    and this works fine.

    but unfortunately if the font like bold, italic, underline... are set to true while calling the
    setStyleSheet(..); I notice that font is loosing all those attributes like bold, underline, italic....

    Is it a limitation of style sheets.

    AFAIK, stylesheets are not good enough. because, if you are interested in changin only the font color, why should one worry about the other attributes like font bold, italic....?

  6. #6
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: using setStyleSheet

    Hi Diams,

    You should read the doc and see that if you use style sheet, it erases everything from QPalette
    and setFont is at the same level as QPalette ( don't ask exactly how does it work)

    but you should take the choice between Qpalette and style sheet

    for font bold:

    Qt Code:
    1. QLabel{color:blue; font:bold;}
    To copy to clipboard, switch view to plain text mode 

    that's all

    david

  7. #7
    Join Date
    Sep 2007
    Posts
    18
    Thanks
    3

    Default Re: using setStyleSheet

    Hi David

    it erases everything from QPalette
    This implementation is not a good idea, isn't it?

    Thanks for the reply

  8. #8
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: using setStyleSheet

    Using style sheet seems the best and powerful way

    otherwise from the doc its is said:

    Do not use this function(setPalette()) in conjunction with Qt Style Sheets. When using style sheets, the palette of a widget can be customized using the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color".

  9. #9
    Join Date
    Sep 2007
    Posts
    18
    Thanks
    3

    Default Re: using setStyleSheet

    how to set the text underline property by using stylesheets?

    QLabel {color:blue; font:bold;}

    the above example works fine for foreground color and font bold attributes.
    But I couldn't see anywehere in Qt documentation about how to set the underline attribute.

    Could anyone help me please?
    -James

  10. #10
    Join Date
    Sep 2007
    Posts
    18
    Thanks
    3

    Default Re: using setStyleSheet

    so, there is no way we can set the
    { font : underline; } attribute through stylesheets???

    NOTE: { font : underline; } fails. This is not the right way of doing it.

    But i dont know the right way either.

  11. #11
    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: using setStyleSheet

    I don't know if Qt supports it, but in CSS it should be "text-decoration: underline;" as far as I remember.

  12. The following user says thank you to wysota for this useful post:

    dyams (21st January 2008)

  13. #12
    Join Date
    Jan 2008
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using setStyleSheet

    Quote Originally Posted by wysota View Post
    I don't know if Qt supports it, but in CSS it should be "text-decoration: underline;" as far as I remember.
    Yes, Qt4.3.x support the most syntax of CSS2, including the "text-decoration: underline;" as you posted

  14. The following user says thank you to jameleon for this useful post:

    dyams (21st January 2008)

Similar Threads

  1. QSpinBox and setStylesheet problem
    By Ace-X in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2007, 11:21

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.