【发布时间】:2015-01-07 06:15:42
【问题描述】:
我需要一个 QPushButton 在文本中有两种颜色。我在 QTextDocument 中找到了一个带有 html 代码的解决方案,它正在工作。但我需要居中对齐,而 html 代码不起作用。
QTextDocument Text;
Text.setHtml("<p align=center><font>Button</font><br/><font color=yellow>1</font></p>");
QPixmap pixmap(Text.size().width(), Text.size().height());
pixmap.fill( Qt::transparent );
QPainter painter(&pixmap);
Text.drawContents(&painter, pixmap.rect());
QIcon ButtonIcon(pixmap);
ui->toolButton->setText("");
ui->toolButton->setIcon(ButtonIcon);
ui->toolButton->setIconSize(pixmap.rect().size());
我知道了,
相同的代码 HTML 在 QLabel 中工作
ui->label->setText("<p align=center><font>Label</font><br/><font color=yellow>1</font></p>");
有什么办法吗?
非常感谢。
【问题讨论】:
-
在QtCreator中,进入设计模式,进入属性子视图,向下滚动,你可以在某处改变对齐方式
-
好的,试试看:
Button
1
还是同样的问题? -
谢谢,但该解决方案也不起作用。相同的代码在 QLabel 中完美运行。 :(
标签: c++ qt qt4 qpushbutton qtextdocument