Betty Crokker
Betty Crokker

Reputation: 3383

Qt5 - setting background color to QPushButton and QCheckBox

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:

pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
palette.setColor(QPalette::Window, QColor(Qt::blue));
pButton->setPalette(palette);
pButton->show();

and if I try changing the style sheet:

pButton->setStyleSheet("background-color: rgb(255,255,0);");

then Qt throws up its hands and draws an afwul-looking blocky button.

There is a page titled "How to change the background color of QWidget" but it just talks about those two methods.

There is also a page "Qt Style Sheets Examples" that implies that if you want to change the background color, you have to take over all aspects of drawing the button, which just seems like overkill.

I need this to run on Mac, Windows, and Ubuntu Linux, and it's really not a happy thing if I have to manually draw everything about the button 3 times (once for each platform).

Am I missing something obvious?

p.s. By "background color" I mean the area surrounding the button, not the color under the text on the face of the button.

Upvotes: 26

Views: 122460

Answers (7)

Tim Seed
Tim Seed

Reputation: 5289

I want to toggle the color of a button On/Off.

This works for me ...

 QPalette pal = ui->pushButton->palette();
 if (bIn)
    pal.setColor(QPalette::Button, QColor(Qt::green));
 else
    pal.setColor(QPalette::Button, QColor(Qt::red));
  ui->pushButton->setPalette(pal);
  ui->pushButton->setAutoFillBackground(true);
 ui->pushButton->repaint();

I flip the value of bIn on a Clicked Signal/Slot.

void BtnFrame::on_pushButton_clicked()
{
    if (bIn)
        bIn=false;
    else
        bIn=true;
    setColor();
}

Upvotes: 0

goodle06
goodle06

Reputation: 11

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

Upvotes: 0

Nicolai Lissau
Nicolai Lissau

Reputation: 8332

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;

Upvotes: 16

whisper
whisper

Reputation: 95

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?

Upvotes: 5

abaghiyan
abaghiyan

Reputation: 159

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.

Upvotes: 15

kato2
kato2

Reputation: 601

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

enter image description here

Upvotes: 11

Alchete
Alchete

Reputation: 1649

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!

Upvotes: 26

Related Questions