问题
I want to use the HTML
ui->FresBox->setText("f<sub>res</sub>");
but it does not work in a QCheckbox
. It works fine if you use a label. What is the different and how can I use the HTML style in a QCheckbox
.
回答1:
Unfortunately, QCheckBox does not support HTML, so in these cases I prefer to use a QCheckBox plus a QLabel in a QHBoxLayout as I show below:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
// start
QCheckBox *checkbox = new QCheckBox();
QLabel *label = new QLabel("f<sub>res</sub>");
QHBoxLayout *hlay = new QHBoxLayout;
hlay->setContentsMargins(0, 0, 0, 0);
// hlay->setSpacing(0);
hlay->addWidget(checkbox, 0);
hlay->addWidget(label, 1);
// end
QVBoxLayout *lay = new QVBoxLayout(&w);
lay->addLayout(hlay);
lay->addWidget(new QCheckBox("plain checkbox"));
w.show();
return a.exec();
}
回答2:
Why not use a disabled ''QTextEdit''? A ''QTextEdit'' should accept rich text (''setAcceptRichText(true)'').
来源:https://stackoverflow.com/questions/58145611/how-i-can-use-sub-sub-in-text-description-from-qcheckbox