Results 1 to 9 of 9

Thread: QPushButton with MANY text color

  1. #1
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default QPushButton with MANY text color

    I know how to do single color, using style sheet or QPalette, but what can i do when button's text has more that one color?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPushButton with MANY text color

    There's a very nice solution here:
    http://www.qtcentre.org/threads/3109...160#post145160

    Basically, use a label or text document to print the text you want.
    Then paint it to a pixmap and use the pixmap on your button.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPushButton with MANY text color

    I've added this to the wiki page:
    http://www.qtcentre.org/wiki/index.p...ultiple_colors)

  4. #4
    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: QPushButton with MANY text color

    This solution surely works, but you lose the possibility to add a custom icon. I think a better approach would be to alter the paint event and render the text document to the painter. After setting html you just need to update the buttons size. Than you can use the icon and all other functions as normally. A similar project is [WIKI]OrientationButton[/WIKI] which uses the paint event.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPushButton with MANY text color

    You're right.
    I'll improve the wiki page.

  6. #6
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QPushButton with MANY text color

    How to do that if button with pixmap is disabled, icon is not gray?
    It is no working..
    code Code:
    1. text.setHtml("<font color = green><b>sometext</b></font>");
    2. pixmap = QPixmap(text.size().toSize());
    3. pixmap.fill(Qt::transparent);
    4. text.drawContents(&QPainter(&pixmap), pixmap.rect());
    5. pixmap = QIcon(pixmap).pixmap(pixmap.size(), QIcon::Disabled);
    6. button->setIcon(QIcon(pixmap));
    7. leftButtons[i]->setIconSize(pixmap.rect().size())
    To copy to clipboard, switch view to plain text mode 
    ;


    Resolved
    Last edited by somename; 30th May 2010 at 12:07.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPushButton with MANY text color

    Here's an update:
    http://www.qtcentre.org/wiki/index.p...ultiple_colors)
    Now you can also set an icon.
    Since my time today is limited, I turned it into an example. The example can use some improvements too.

  8. #8
    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: QPushButton with MANY text color

    Quote Originally Posted by tbscope View Post
    Since my time today is limited, I turned it into an example. The example can use some improvements too.
    Thank you, tbscope, for spending so much time to write wiki articles! Just two small notes for further improvements: Creating a QTextDocument in each paint event is too heave. Better store a private pointer. Further, there is no need to create a pixmap and paint it, you can directly use the painter: QTextDocument::drawContents().

    I just remember, that wysota also have a rich text button in his wwWidgets: http://www.wysota.eu.org/wwwidgets/d...extbutton.html.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPushButton with MANY text color

    Quote Originally Posted by Lykurg View Post
    I just remember, that wysota also have a rich text button in his wwWidgets: http://www.wysota.eu.org/wwwidgets/d...extbutton.html.
    Ohh, nice. I didn't know that.
    It was fun to do anyway.

Similar Threads

  1. QPushButton color and background
    By satanowicz in forum Newbie
    Replies: 4
    Last Post: 25th May 2010, 20:56
  2. Replies: 3
    Last Post: 22nd January 2010, 16:46
  3. QPushbutton background color
    By omega36 in forum Qt Programming
    Replies: 27
    Last Post: 31st October 2008, 12:47
  4. QPushButton color when clicked
    By ntp in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2008, 18:34
  5. QPushbutton + QLabel text color
    By Ashish in forum Qt Programming
    Replies: 5
    Last Post: 3rd February 2007, 08:13

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.