如何在Qt的QRadioButton中设置下标、上标等特殊字符
发布于 2017-02-25 06:31:24
您可以尝试重写paintEvent() of QRadioButton (但您必须将其子类化),并使用QPainter绘制文本。以下是您可以使用的示例。我试过了,除了尺寸问题外,效果还不错:

我的单选按钮的重写paintEvent(QPaintEvent* event)是:
QRadioButtonRichtTextSupport.h
void QRadioButtonRichtTextSupport::paintEvent(QPaintEvent* event)
{
// First draw the original content of the radio button - the circle and the plain text
QRadioButton::paintEvent(event);
// Get the rectangle of the paint event
QRect _rect = event->rect();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// Erase the text - I have used a translation of 16 along the X axis
// from the top left corner of the paint rectangle but there might be
// some less dirty way of doing this. Basically this is used to leave
// the circle of the radio button intact while erasing the text part
painter.eraseRect(_rect.topLeft().x()+16, _rect.topLeft().y(), _rect.width()-16, _rect.height());
// Translate the painter along the X axis with 16
painter.translate(QPointF(16, 0));
// Create a text document which supports rich text formatting
QTextDocument label;
label.setHtml(this->text());
// and finally draw the text over the radio button starting from +16 along the X axis
label.drawContents(&painter);
painter.end();
}Widget.cpp
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setLayout(&this->layout);
// Create the custom radio button. Notice that I've also added a string argument (not present in the default constructor of QRadioButton)
// to add the ability to set the text upon initialization. This is completely optional
QRadioButtonRichtTextSupport* rbrt = new QRadioButtonRichtTextSupport("<b>Rich text</b><sup>abc123</sup>", this);
this->layout.addWidget(rbrt);
}这里的问题是,我确实设置了单选按钮的文本(使用setText(...)),而且由于单选按钮不支持丰富的文本格式,所以您可以得到一个更宽的单选按钮(上面图像中文本后面的空空间实际上是原始文本的位置(在我的例子中是:<b>Rich text</b><sup>abc123</sup>)。您将不得不查看更多的这一点,看看如何调整大小,以减少“额外”的空间。也可以通过重写自定义单选按钮的resizeEvent(QResizeEvent* event)来实现。您可以使用label.documentLayout()->documentSize()获取正在使用的QTextDocument的大小,然后添加额外的空间(在我的例子中,我沿着X轴使用+16,这会改变的宽度)。
如果由于某种原因,这不是一个选项,那么复合小部件似乎是最简单的方法--创建一个具有水平布局的小部件,首先放置一个没有文本的QRadioButton,然后是一个带有单选按钮文本的QLabel。
UTF-8的问题在于它只支持一个上标拉丁语子集 (和希腊语)字母(例如,当你想写迷信的q时,你就完了)。在UTF-16中可能有一些更完整的东西( Qt也支持它),所以如果创建自己的QRadioButton或创建一个复合小部件对您来说不是最理想的,那么您可能想研究一下。
发布于 2017-02-25 05:45:14
根据文档,QRadioButton没有为其标签提供丰富的文本格式(例如,QLabel)。令人惊讶!
所以,这就是我试过的:
tgl1 ...单选按钮,包含丰富的文本,如QLabel,但不是预期的tgl2 ...使用Unicodes (只要有必要的字符,成功就有限)tgl3...从多个小部件组合单选按钮QRadioButton以覆盖绘图。备选案文3和4是最普遍的办法。(应该做一些额外的努力来改进3.选项的布局。4.我作为练习留下的选项.)
样本代码:
#include <QApplication>
#include <QRadioButton>
#include <QButtonGroup>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QMainWindow>
#include <QVBoxLayout>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow win;
QGroupBox box;
QVBoxLayout vBox;
// radio button with mark-up
QRadioButton qTgl1(QString::fromLatin1("Radio<sub>1</sub>"));
vBox.addWidget(&qTgl1);
// radio button exploiting Unicode extensively
QRadioButton qTgl2(QString::fromUtf8("Radio\342\202\202"));
// '2' as subscript ... U+2082 -> \342\202\202
vBox.addWidget(&qTgl2);
// fake a radio button with rich text by composition of widgets
QHBoxLayout hBox3;
QRadioButton qTgl3;
hBox3.addWidget(&qTgl3);
QLabel qLbl3(QString::fromLatin1("Radio<sub>3</sub>"));
qLbl3.setTextFormat(Qt::RichText);
hBox3.addWidget(&qLbl3);
vBox.addLayout(&hBox3);
box.setLayout(&vBox);
win.setCentralWidget(&box);
win.show();
return app.exec();
}这就是它的外观(在Windows 10中):

关于2.选项:有一些网页提供了关于可用Unicode字符(及其代码点)的概述。
一些Unicode代码点示例:
U+2070 . U+2079:数字0 . 9在上标U+2080 . U+2089:数字0 . 9在下标U+208A . U+208E:下标中的字符"+-=()"也许还有其他人..。
https://stackoverflow.com/questions/42451712
复制相似问题