Qt / c ++ - set background of a disabled button?

I have all my buttons disabled in the grid, but for some I would like to change the background color. I'm trying to:

_fieldButtons[0][0]->setStyleSheet("color: blue; background-color: blue;");

      

Where

QVector<QVector<QPushButton*> > _fieldButtons;

      

However, these buttons are disabled and only the text color changes:

enter image description here

How do I change the background but not the text? Thank!

UPDATE I figured it doesn't work because the buttons are flat. How do I change the color of a button?

+3


source to share


3 answers


Two options:

  • Add border: none;

    to style sheet.

  • Use setAutoFillBackground(true)

    in conjunction withQPalette::Button

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



http://doc.qt.io/qt-5/qpushbutton.html#flat-prop

0


source


Try the following:



myButton->setPalette(QColor("#124e6b"));

0


source


Here you have two " Pseudo-States " in your control. The list of " Pseudo-States " is shown below.

http://doc.qt.io/qt-5/stylesheet-reference.html#list-of-pseudo-states

The first "pseudo-state" is flat

Second "Pseudo-state" - disabled

Here you need to lock both states in order to set the style using "setStyleSheet".

_fieldButtons[0][0]->setStyleSheet(":flat:disabled {background-color: blue; border: none;}");

      

search for ": hover: enabled" (two different " Pseudo-States " as the documentation is handled) in the link below to get a better idea.

http://doc.qt.io/qt-4.8/stylesheet-syntax.html

To understand why we have to give border:none

for QPushButton

, in the first hyperlink in this answer you will find the information below.

"Warning. If you only set the background color in a QPushButton, the background may not be displayed unless you set the border property. This is because, by default, QPushButton draws its own border that completely overlaps the background color."

-1


source







All Articles