Sarah Akhavan
Sarah Akhavan

Reputation: 247

html tags are not working in Qt

I'm learning how to use html and css in Qt programming and I want to change the text of a pushButton using this :

ui->pushButton->setText("<b>chert</b>");

But it doesn't work correctly. html tags have no effect on it. How can I make it work?!

Thanks :)

Upvotes: 2

Views: 1014

Answers (1)

Nejat
Nejat

Reputation: 32665

As a 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 :

QTextDocument doc;
doc.setHtml("<b>chert</b>");
doc.setTextWidth(doc.size().width());

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

button->setIconSize(pixmap.size());
button->setIcon(pixmap);

An other way to achieve this is to derive from QPushButton and draw text yourself via QPainter in paintEvent. You can find a rich text push button implementation here.

You can also use QxtPushButton class from libqxt. QxtPushButton widget is an extended QPushButton with rotation and rich text support.

Upvotes: 2

Related Questions