Qt5 - setting background color to QPushButton and QCheckBox

后端 未结 6 683
小蘑菇
小蘑菇 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:06

    I found a stupid way, tried every attribute in palette, and it works for me when changing 'QPalette::Base'. Maybe you can have a try.

        pButton->setAutoFillBackground(true);
        QPalette palette = pButton->palette();
        //palette.setColor(QPalette::Window, QColor(Qt.blue));
        //palette.setColor(QPalette::Foreground, QColor(Qt.blue));
        palette.setColor(QPalette::Base, QColor(Qt.blue));
        //palette.setColor(QPalette::AlternateBase, QColor(Qt.blue));
        //palette.setColor(QPalette::ToolTipBase, QColor(Qt.blue));
        //palette.setColor(QPalette::ToolTipText, QColor(Qt.blue));
        //palette.setColor(QPalette::Text, QColor(Qt.blue));
        //palette.setColor(QPalette::Button, QColor(Qt.blue));
        //palette.setColor(QPalette::ButtonText, QColor(Qt.blue));
        //palette.setColor(QPalette::BrightText, QColor(Qt.blue));
        pButton->setPalette(palette);
        pButton->show();
    

    reference link : How to get a stylesheet property?

提交回复
热议问题