本文介紹了QPushButton 中的兩種顏色文本的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要一個文本中有兩種顏色的 QPushButton.我在 QTextDocument 中找到了一個帶有 html 代碼的解決方案,并且它正在運行.但是我需要居中對齊并且 html 代碼不起作用.
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 working.
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 中工作
The same code HTML is working in a QLabel
ui->label->setText("<p align=center><font>Label</font><br/><font color=yellow>1</font></p>");
有什么解決辦法嗎?
非常感謝.
推薦答案
你可以從QPushButton派生出來,通過paintEvent中的QPainter自己繪制文本.
You can derive from QPushButton and draw text yourself via QPainter in paintEvent.
class Button : public QPushButton
{
Q_OBJECT
public:
Button(QWidget *parent = 0)
: QPushButton(parent)
{ }
void paintEvent(QPaintEvent *p)
{
QPushButton::paintEvent(p);
QPainter paint(this);
paint.drawText(QPoint(10,10),"Hello");
}
};
這篇關于QPushButton 中的兩種顏色文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!