Two colours text in QPushButton

As s workaround you can use a label or text document to print the text you want. You should paint it to a pixmap and use the pixmap on your button :

QPushButton *button = new QPushButton(this);
QTextDocument Text;
Text.setHtml("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");

QPixmap pixmap(Text.size().width(), Text.size().height());
pixmap.fill( Qt::transparent );
QPainter painter( &pixmap );
Text.drawContents(&painter, pixmap.rect());

QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());

You can also find a rich text push button implementation here.

An other option is to use QxtPushButton class from libqxt. QxtPushButton widget is an extended QPushButton with rotation and rich text support.

Leave a Comment