html 标签在 Qt 中不起作用

标签 html c++ qt

我正在学习如何在 Qt 编程中使用 html 和 css,我想使用它来更改按钮的文本:

ui->pushButton->setText("<b>chert</b>");

但它无法正常工作。 html 标签对其没有影响。 我怎样才能让它工作?!

谢谢:)

最佳答案

作为解决方法,您可以使用标签或文本文档来打印所需的文本。您应该将其绘制到像素图并在按钮上使用该像素图:

QTextDocument doc;
doc.setHtml("<b>chert</b>");
doc.setTextWidth(doc.size().width());

QPixmap pixmap(doc.size().width(), doc.size().height());
pixmap.fill( Qt::transparent );
QPainter painter(&pixmap);
doc.drawContents(&painter);

button->setIconSize(pixmap.size());
button->setIcon(pixmap);

实现此目的的另一种方法是从 QPushButton 派生并通过 paintEvent 中的 QPainter 自行绘制文本。您可以找到富文本按钮实现 here .

您还可以使用QxtPushButton来自 libqxt 的类。 QxtPushButton 小部件是一个扩展的 QPushButton,具有旋转和富文本支持。

关于html 标签在 Qt 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063377/

相关文章:

css - 使父 div 高度反射(reflect)子 div 的填充/边框/边距的好方法是什么?

c++ - 使用 OpenAL 的硬件加速音频解码

c++ - 如何在 ms-dos 控制台中用 C++ 编写 unicode 字符波斯语?

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 在 Windows 上首次安装 Qt 5

HTML5 : Is iPhone4's hardware acceleration that much weaker than compared to the 4s?

html - tabIndex 不会使用 Tab 键使标签可聚焦

javascript - 使用 JQuery 仅在该 div 中显示/隐藏按钮

c++ - 在Qt中,发现所有窗口都关闭了,当使用QApplication::processEvents()时?

c++ - Qt : C++ dynamic parameter GUI for plugin parameters