pythonqt-designerpyside2

Changing the appearance of a QT checkbox indicator


I need to change how the checkable box of a QCHeckBox looks, and not simply with CSS, but with a texture file. The closest thing I found was changing the icon parameter of the widget.

But a) it doesn't update, and b) It is located after the box, which I'm actually trying to change. Is it possible to change the checkbox itself?


Solution

  • Confusingly, those icon properties are associated with the QAbstractButton parent class, which doesn't have the concept of a checkbox icon. So they just add an icon to the label of the button, which results in the behaviour you describe.

    To change the actual checkbox icon, the best option I've found is to use a stylesheet. All the gory details are here.

    In summary, something like this will do as the QCheckBox's stylesheet:

    QCheckBox::indicator:unchecked {
        image: url(:/images/checkbox_unchecked.png);
    }
    
    QCheckBox::indicator:checked {
        image: url(:/images/checkbox_checked.png);