10

Is there a way to wrap text in QCheckBox like it is done with QLabel?

label = QLabel( QString.fromUtf8('long text in here'))
label.setWordWrap(True)

I tried \n which will add a linebreak but this is not dynamic if I resize the window.

Germar
  • 436
  • 4
  • 24
  • It seems there is a feature request for this at https://bugreports.qt-project.org/browse/QTBUG-5370. The issue is not closed, so likely this feature is not implemented. – sashoalm May 09 '13 at 07:21
  • 1
    See [this](http://stackoverflow.com/questions/1839194/qcheckbox-qradiobutton-line-wrap-qt4-6-0) question, it has at least two options to use for you. – Denis Malinovsky May 09 '13 at 07:23

4 Answers4

9

It seems there is a feature request for this at https://bugreports.qt.io/browse/QTBUG-5370.

The issue is not closed, so likely this feature is not implemented. This means that it is not currently possible to add word wrap to QCheckBox.

You can try various workarounds, such as having an empty-text QCheckBox and a QLabel to the right, or try putting a shorter text for the checkbox, with a label below it with the long explanation.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Another solution would be to just use a tooltip. The purpose of a tooltip is for describing in detail. Unfortunately, many users are not familiar with looking for tooltips. – Nicholas Miller Jun 14 '16 at 13:48
  • It's possible to implement this in a clean way by handling the `resizeEvent` within a custom `QCheckBox` subclass, and to perform the necessary line wrapping there. See my answer for details. – emkey08 Feb 08 '23 at 13:50
3

You can develop a Custom widgets for QtDesigner to use this feature graphically (or not).

CheckBoxWordWrap (dynamic when resize):

CheckBoxWordWrap

In QtDesigner:

CheckBoxWordWrap plugin
CheckBoxWordWrap properties

You can also use the class and modify it if the behavior is not exactly what you want.

thibsc
  • 3,747
  • 2
  • 18
  • 38
0

"This is my double \n line checkbox". Now you have double line checkbox

Double Line Checkbox

amer
  • 1,528
  • 1
  • 14
  • 22
0

There unfortunately is no short and simple solution for this. We have to implement this on our own.

The complete implementation of a LineWrappedRadioButton class is already provided in this related answer. You can easily covert this code into a LineWrappedCheckBox class by simply deriving from QCheckBox instead of from QRadioButton there.

Use it like this:

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    LineWrappedCheckBox checkBox("Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.");
    checkBox.show();
    return app.exec();
}

This will produce the following result:

LineWrappedCheckBox screenshot

emkey08
  • 5,059
  • 3
  • 33
  • 34