Flat QPushButton, background-color doesn't work
Asked Answered
R

2

9

I created QPushButton in Qt Designer with this stylesheet:

QPushButton#pushButton { 
    background-color: #ffffff;
}
QPushButton#pushButton:disabled {
    background-color: yellow; 
}
QPushButton#pushButton:pressed {
    background-color: orange; 
}
QPushButton#pushButton:focus:pressed { 
    background-color: black;
 }
QPushButton#pushButton:focus { 
    background-color: green; 
}
QPushButton#pushButton:hover {
     background-color: red;
 }
QPushButton#pushButton:checked { 
    background-color: pink;
 }

It Works, but when i check "flat" in the properties, then it doesn't work anymore, I would like to ask you why? And how can i do it, if i need flat QPushButton ?

Ravenous answered 7/1, 2016 at 11:56 Comment(0)
S
18

For the second part of your question: Don't use the "flat" property, but modify your stylesheet to achieve a flat look, perhaps like this:

QPushButton#pushButton { 
    background-color: #ffffff;
    border: 0px;
}
/* ... plus the rest of your stylesheet ... */

Concerning the "why doesn't it work" part? In my experience, mixing stylesheets and non-stylesheet-options for widgets yields many mysterious effects that are hard to explain. I have given up asking for the "why"s.

Sagittarius answered 7/1, 2016 at 12:9 Comment(1)
If you set border:0px; the state of the flat property will not affect the looks.Toritorie
T
1
// Send Button
m_send_button = new QPushButton("Send", this);
m_send_button->setFlat(true);
m_send_button->setStyleSheet(QString::fromUtf8("QPushButton:flat {"
                                               "color: #FFFFFF;"
                                               "border: 0px;" // or border: none;
                                               "background-color: #F39200;"
                                               "}"));
Teem answered 26/11, 2022 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.