Qt5 - setting background color to QPushButton and QCheckBox

隐身守侯 提交于 2019-11-29 03:43:40

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!

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)
}

In the stylesheet add border:none; property. For some reason this removes the default background color also. Example: background-color: rgba(46, 204, 113, 0.4); border: none;

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.

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!