PDA

View Full Version : QPushButton with MANY text color



somename
30th May 2010, 09:18
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?

tbscope
30th May 2010, 09:25
There's a very nice solution here:
http://www.qtcentre.org/threads/31094-QPushButton-with-a-custom-QLabel?p=145160#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.

tbscope
30th May 2010, 10:17
I've added this to the wiki page:
http://www.qtcentre.org/wiki/index.php?title=Buttons_with_richt_text_(multiple_ colors)

Lykurg
30th May 2010, 10:40
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 OrientationButton which uses the paint event.

tbscope
30th May 2010, 10:55
You're right.
I'll improve the wiki page.

somename
30th May 2010, 11:51
How to do that if button with pixmap is disabled, icon is not gray?
It is no working..

text.setHtml("<font color = green><b>sometext</b></font>");
pixmap = QPixmap(text.size().toSize());
pixmap.fill(Qt::transparent);
text.drawContents(&QPainter(&pixmap), pixmap.rect());
pixmap = QIcon(pixmap).pixmap(pixmap.size(), QIcon::Disabled);
button->setIcon(QIcon(pixmap));
leftButtons[i]->setIconSize(pixmap.rect().size());


Resolved

tbscope
30th May 2010, 13:36
Here's an update:
http://www.qtcentre.org/wiki/index.php?title=Buttons_with_richt_text_(multiple_ 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.

Lykurg
30th May 2010, 14:25
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/doc/html/qwwrichtextbutton.html.

tbscope
30th May 2010, 14:38
I just remember, that wysota also have a rich text button in his wwWidgets: http://www.wysota.eu.org/wwwidgets/doc/html/qwwrichtextbutton.html.

Ohh, nice. I didn't know that.
It was fun to do anyway.