Qt5 - setting background color to QPushButton and QCheckBox

后端 未结 6 678
小蘑菇
小蘑菇 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?

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-06 05:13

    I had the same issue, but finally got this to work. I'm using Qt 5 with the Fusion color theme:

    QPalette pal = button->palette();
    pal.setColor(QPalette::Button, QColor(Qt::blue));
    button->setAutoFillBackground(true);
    button->setPalette(pal);
    button->update();
    

    Try these commands in the exact order as above, and if that still doesn't work, set your theme to Fusion and try again.

    Good luck!

    0 讨论(0)
  • 2020-12-06 05:17

    Changing the Dialog styleSheet Property works for me, set this property to:

    QPushButton:pressed {
        background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,   stop:0 rgba(60, 186, 162, 255), stop:1 rgba(98, 211, 162, 255))
    }
    QPushButton {
         background-color: #3cbaa2; border: 1px solid black;
         border-radius: 5px;
    }
    
    QPushButton:disabled {
        background-color: rgb(170, 170, 127)
    }
    

    0 讨论(0)
  • 2020-12-06 05:18

    Add propoerty border:none; to the stylesheet

    For some reason this removes the default background color also.

    Example: background-color: rgba(46, 204, 113, 0.4); border: none;

    0 讨论(0)
  • 2020-12-06 05:32

    Try this:

    QColor col = QColor(Qt::blue);
    if(col.isValid()) {
       QString qss = QString("background-color: %1").arg(col.name());
       button->setStyleSheet(qss);
    }
    

    as mentioned at the QT Forum by @goetz.

    I used some different definition of Qcolor col as QColor col = QColor::fromRgb(144,238,144); and this works for me.

    0 讨论(0)
提交回复
热议问题