Two colours text in QPushButton

后端 未结 3 436
失恋的感觉
失恋的感觉 2020-12-20 21:06

I need a QPushButton with two colors in the text. I found a solution with a html code in QTextDocument and it\'s working. But I need center align and the html code isn\'t wo

3条回答
  •  情歌与酒
    2020-12-20 21:18

    There is a bug report about this. When you use QTextDocument::drawContents, it tends to ignore the aligments. To make it work, set the text width using QTextDocument::setTextWidth.

    QTextDocument doc;
    doc.setHtml("

    Button
    1

    "); doc.setTextWidth(doc.size().width()); QPixmap pixmap(doc.size().width(), doc.size().height()); pixmap.fill( Qt::transparent ); QPainter painter(&pixmap); doc.drawContents(&painter); QPushButton button; button.setIconSize(pixmap.size()); button.setIcon(pixmap); button.show();

提交回复
热议问题