Results 1 to 5 of 5

Thread: QColor with style sheets

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QColor with style sheets

    I have a question about how to use QColor with style sheets.
    Normally I would color my text by doing:

    Qt Code:
    1. QLabel Label;
    2. Label->setStyleSheet( "color: rgb(100,200,200);" );
    To copy to clipboard, switch view to plain text mode 

    Now I'd like to specify the rgb color in QColor format (as shown below) but that won't work:

    Qt Code:
    1. QLabel Label;
    2. mycolor = QColor(100,200,200);
    3. x1Label->setStyleSheet( "color: mycolor;" );
    To copy to clipboard, switch view to plain text mode 

    I think I can not put QColor directly into the stylesheet.
    So I also tried color: mycolor.toRgb(); but still didn't get it to work

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QColor with style sheets

    So you want to dynamically specify colors... Well, you can't do it like that.
    You can do it with a QString and placeholders:
    Qt Code:
    1. QString style = "color: rgb(%1, %2, %3);";
    2. label->setStyleSheet(style.arg(myColor.red()).arg(myColor.green()).arg(myColor.blue()));
    To copy to clipboard, switch view to plain text mode 
    Last edited by marcel; 26th December 2007 at 22:31.

  3. The following user says thank you to marcel for this useful post:

    tommy (26th December 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QColor with style sheets

    Thanks Marcel,

    I got your code to work but I don't know what the syntax is to combine what you suggested into the same style sheet with the other style declarations. For example both of the style sheet declarations below work but I can't put them together into one. What is the syntax? Thanks a lot!

    Qt Code:
    1. mycolor = QColor(100,200,200);
    2.  
    3. QString style = "background: rgb(%1, %2, %3);";
    4.  
    5. drawButton->setStyleSheet(style.arg(mycolor.red()).arg(mycolor.green()).arg(mycolor.blue()));
    6.  
    7.  
    8. drawButton->setStyleSheet(
    9. "color:black; font-size:12px;"
    10. "font-weight:bold;"
    11. );
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QColor with style sheets

    With the label colour it would be much simpler to use the palette...
    Qt Code:
    1. QLabel label;
    2. QPalette palette = label.palette();
    3. palette.setColor(QPalette::Foreground, mycolor);
    4. label.setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

  6. The following 2 users say thank you to wysota for this useful post:

    Momergil (26th March 2014), tommy (27th December 2007)

  7. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QColor with style sheets

    I got your code to work but I don't know what the syntax is to combine what you suggested into the same style sheet with the other style declarations. For example both of the style sheet declarations below work but I can't put them together into one. What is the syntax? Thanks a lot!
    Because the style sheet parameter is a QString, you can use all QString features, like the "+" and "+=" operators:
    Qt Code:
    1. mycolor = QColor(100,200,200);
    2. QString style = "background: rgb(%1, %2, %3);";
    3. style = style.arg(mycolor.red()).arg(mycolor.green()).arg(mycolor.blue());
    4. style += "color:black; font-size:12px;";
    5. style += "font-weight:bold;";
    6. drawButton->setStyleSheet(style);
    To copy to clipboard, switch view to plain text mode 

  8. The following 2 users say thank you to marcel for this useful post:

    giowck (3rd May 2012), tommy (27th December 2007)

Similar Threads

  1. BIG Problem with Background-image using Style Sheets
    By PiXeL16 in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:10
  2. BG image on QPushButton using style sheets??
    By JimDaniel in forum Newbie
    Replies: 2
    Last Post: 13th September 2007, 03:31
  3. Qt style sheets
    By locus in forum Qt Programming
    Replies: 5
    Last Post: 5th April 2007, 17:03
  4. style sheets
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2007, 15:14
  5. Qt Style Sheets Problems with QDialog
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 5th November 2006, 14:43

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
  •  
Qt is a trademark of The Qt Company.