Two colours text in QPushButton(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 中的两种颜色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QPushButton 中的两种颜色文本


基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++,'if' 表达式中的变量声明 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17