Qt5 - setting background color to QPushButton and QCheckBox

后端 未结 6 697
小蘑菇
小蘑菇 2020-12-06 05:01

I\'m trying to change the background color of a QAbstractButton (either a QPushButton or QCheckBox) in Qt5 and having zero luck.

This does nothing:

p         


        
6条回答
  •  广开言路
    2020-12-06 05:08

    I've struggled with same problem. You need to choose QPalette::Button instead of QPalette::Window. Qt reference says this: QPaletteButton - The general button background color. This background can be different from Window as some styles require a different background color for buttons. So I solved it this way:

            QPalette pal=this->palette(); \\"this" is my derived button class
            pal.setColor(QPalette::Window, style.background);
            QColor col=style.background; \\ my style wrapper, returns QColor
            this->setAutoFillBackground(true);
            this->setPalette(pal);
    

提交回复
热议问题