1

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.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130

2 Answers2

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();
}

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • @MichaelBaldischweiler If my answer helped you then do not forget to mark as correct. If you don't know how to do it then check the [tour] – eyllanesc Sep 30 '19 at 06:25
  • It should be noted that one of the main negatives of this approach is that you can no longer click on the text to also toggle the checked state. There are still ways to achieve this, but it's non-trivial since QLabel does not have a "clicked" signal. – btse Oct 10 '22 at 17:00
0

Why not use a disabled ''QTextEdit''? A ''QTextEdit'' should accept rich text (''setAcceptRichText(true)'').

Ben
  • 1,519
  • 23
  • 39