Changing QCheckBox indicator rectangle color

21,071

Solution 1

There is no need to create a custom delegate for this. You could use stylesheets in order to change the color of the checkbox or any othe widget as you wish. The following stylesheet will set a gray 3px border to the checkbox rectangle.

QCheckBox::indicator {
    border: 3px solid #5A5A5A;
    background: none;
}

In order to set the stylesheet you could either use the Qt Designer or the setStylesheet function.

In order to set a specific color all of the following are valid:

border: 3px solid #5A5A5A;
border: 3px solid red;
border: 3px solid rgb(255, 120, 100);
border: 3px solid rgba(255,120,100, 50); // For alpha transparency

Solution 2

The simplest way I have been able to figure out is use a QApplication color palette to help change the checkbox indicator color. This makes it so you don't have to override the entire checkbox widget with all the states with QStyle.

This is the type of code that did what I needed

QPalette newPallete = myWidget->palette();
newPallete.setColor(QPalette::Active, QPalette::Background, myWidget->palette().text())
myWidget->setPalette(newPallete)

See this other example with something very similar to styling buttons: Qt5 - setting background color to QPushButton and QCheckBox

Share:
21,071
Simon
Author by

Simon

Qt/C++

Updated on July 09, 2022

Comments

  • Simon
    Simon almost 2 years

    I'm trying to change only the color of QCheckBox indicator rectangle.

    Currently I succeed to draw the right and the bottom line of the rectangle. Probably I'm doing something wrong here.

    Here is my code:

    CheckBoxWidget.cpp

    CheckBoxWidget::CheckBoxWidget(QObject *poParent)
        : QItemDelegate(poParent)
    {
    }
    
    void CheckBoxWidget::drawCheck( QPainter *painter,
                                    const QStyleOptionViewItem &option,
                                    const QRect & rect,
                                    Qt::CheckState state) const
    {
        QRect oCheckBoxRect =
                QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &option);
    
        painter->setPen(Qt::white);
        painter->drawRect(oCheckBoxRect);
    
        QItemDelegate::drawCheck(painter, option, oCheckBoxRect, state);
    }
    

    CheckBoxWidget.h

    class CheckBoxWidget : public QItemDelegate
    {
        Q_OBJECT
    
    public:
        CheckBoxWidget(QObject *poParent = 0);
        virtual ~CheckBoxWidget();
    
    protected:
        virtual void drawCheck( QPainter *painter,
                                const QStyleOptionViewItem &option,
                                const QRect &,
                                Qt::CheckState state) const;
    };
    

    Any suggestions ?

  • Anthony Hilyard
    Anthony Hilyard over 8 years
    The problem with this approach is that it changes the entire appearance of the check box, not just the border color as requested. When setting a custom stylesheet on Qt elements, you need to supply the style for every part of the element.
  • Dariusz
    Dariusz over 7 years
    Sadly same issue as above. I set my disabeld qcheckbox background to gray but I no longer see the rest of checkbox - The tick mainly. How can I style tick? Same goes for QRadioButton. Changing style there makes the radio button square :- ( Any ideas where to find info on styling these buttons?
  • nostradamus
    nostradamus over 7 years
    Facing the same problem now... Is there any progress on this?
  • vitperov
    vitperov over 6 years
    The same issue: setting indicator size works, but when I try to set border color checked state looks like unchecked
  • lenooh
    lenooh about 6 years
    If you change one property, you have to set them all. Here is the list of all the properties: doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcheckbo‌​x
  • greendino
    greendino almost 4 years
    how is this get upvoted when i cannot see any indicator tick on my checkbox!