0

I have the problem that the background of the button cannot be changed. Only the border is changed. I assumed that some attribute prevented this. But I think I'm wrong on that point. I've gone through the Qt documentation but can't find anything. I can only find examples on the internet that give me the same result. Is there a way to change the background?

Here is the code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //ui->pushButton->setAttribute(Qt::WA_SetPalette);
    //ui->pushButton->setAttribute(Qt::WA_SetStyle);

    ui->pushButton->setAutoFillBackground(true);
    ui->pushButton->setAttribute(Qt::WA_ShowModal);
    QPalette pal = ui->pushButton->palette();

    //pal.setColor(QPalette::Base, Qt::cyan);
    //pal.setBrush(QPalette::Base, Qt::cyan);
    pal.setColor(QPalette::Button, Qt::cyan);
    pal.setBrush(QPalette::Button, Qt::cyan);
    
    ui->pushButton->setPalette(pal);

    ui->pushButton->update();

    //ui->pushButton->setAttribute(Qt::WA_NoSystemBackground, true);

//    setAutoFillBackground(true);
//    QPalette pal2 = palette();
//    pal2.setBrush(QPalette::Button, Qt::cyan);
//    pal2.setColor(QPalette::ButtonText, Qt::cyan);
//    QApplication::setPalette(pal2);
}

enter image description here

BanAnn
  • 159
  • 1
  • 7

2 Answers2

2

As I test your code this is my result:

enter image description here

But you can change it By using StyleSheet instead of QPalette.

ui->pushButton->setStyleSheet(QString::fromUtf8("background-color: cyan;"));
Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • Yes I know. But I wanted to try QPalette. Can otherwise be due to any settings in qt itself? I can change everything else except the buttons – BanAnn Sep 17 '22 at 21:30
  • @BanAnn I think this relates to your OS because I can see changes without changing anything I use Qt 6.3 but my OS is ubuntu 20.04. – Parisa.H.R Sep 17 '22 at 21:32
  • 1
    But I'll vote. Without your answer I would have continued reading documentation, although it could be something else. so thank you in advance for the hint. maybe someone had the same problem. – BanAnn Sep 17 '22 at 21:45
0

Here's the code I use to set the background color of a QPushButton (or any QAbstractButton):

// @param btn button to change colors of
// @param bc new background color for button
// @param optTextColor if non-NULL, the new color to use for the
//                button's text label.  If NULL, this function
//                will choose either black or white (whichever
//                it thinks will be more readable)
void SetButtonColor(QAbstractButton * btn, const QColor & bc, const QColor * optTextColor = NULL)
{
   QPalette p = btn->QWidget::palette();
   p.setColor(QPalette::Button, bc);

   const QColor fc = GetContrastingTextColor(bc);
   p.setColor(QPalette::Active,   QPalette::ButtonText, optTextColor?*optTextColor:fc);
   p.setColor(QPalette::Inactive, QPalette::ButtonText, optTextColor?*optTextColor:fc);
   p.setColor(QPalette::Disabled, QPalette::ButtonText, optTextColor?*optTextColor:MixColors(fc, Qt::lightGray, 0.5f));
   btn->setPalette(p);
}

// Returns either Qt::white or Qt::black, whichever will make for more readable text
// in front of the passed-in background color
QColor GetContrastingTextColor(const QColor & c)
{     
   const int darkThresh = 64;
   const int loneDelta  = 129;
   const int loneRed    = ((c.green()<darkThresh)&&(c.blue() <darkThresh)) ? loneDelta : 0;
   const int loneGreen  = 0;
   const int loneBlue   = ((c.red()  <darkThresh)&&(c.green()<darkThresh)) ? loneDelta : 0;
   return (std::max(c.red()-loneRed, std::max(c.green()-loneGreen, c.blue()-loneBlue)) >= 128) ? Qt::black : Qt::white;
}

// Returns a weighted average value between (v1) and (v2)
static int Mix(int v1, const int v2, float p) 
{
   return (int) ((((float)v2)*p)+(((float)v1)*(1.0f-p)));
}

// Returns a weighted average value between (c1) and (c2)
QColor MixColors(const QColor & c1, const QColor & c2, float p) 
{
   return QColor(Mix(c1.red(), c2.red(), p), Mix(c1.green(), c2.green(), p), Mix(c1.blue(), c2.blue(), p));
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Thanks for the answer. But unfortunately that doesn't work for me either (only the text color is changed). After some research I found (and also as @Parisa.H.R suggested ) that it is probably due to Windows that you cannot change the color. If you are using Windows, it could be my settings in qt. Maybe there are any hints? I couldn't find anything regarding this – BanAnn Sep 18 '22 at 11:10
  • 2
    I suggest doing a `qApp->setStyle(QStyleFactory::create("windows"));` at startup (after declaring your `QApplication` object) and seeing what difference that makes. – Jeremy Friesner Sep 18 '22 at 14:01