QPushButton 中的两种颜色文本

编程入门 行业动态 更新时间:2024-10-10 09:20:31
本文介绍了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 中的两种颜色文本

本文发布于:2023-11-28 21:55:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   文本   颜色   QPushButton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!