How to know what's the current color background of a pushButton?

  • I put a pushButton like a toggle button and everytime a click(is. toggled), change the color to green and when I "distoggled" the color back to transparent.

    void gui_1::on_pushButton_4_clicked()

    QPalette palette;
    palette.setColor(ui.pushButton_4->backgroundRole(), Qt::red);

    if I just do this the button will be toggled only in the second clicked because call first the click, to solve this I called the clicked method in the main:
    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    gui_1 w;;

    return a.exec();


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not set the palette in the constructor of your widget ?

  • @SGaist why do I need this?
    I'm new using Qt C++, so..
    I could put the pushButton as a toggleButton, mas I need to change the color every time i toggle, but i couldn't, because I think I need to do something like:

    something like that....

  • @SGaist I need to know how can i get the current color to be able change the color....

  • Lifetime Qt Champion

    You rather need to know the state of your button. You should also be able to achieve this using style sheets

  • Qt Champions 2016

    As SGaist is saying:

    Using a style sheet is a bit easier as you do not need to keep track of state.
    As I understand it, you want to have a different color when "checked" ?

    If you right click a push button select "Change style sheet" and paste the following

    QPushButton {
    background-color: red;
    border-style: solid;
    border-width: 3px;
    border-radius: 75px 50px;
    border-color: blue;
    font: bold 14px;
    padding: 6px;

    QPushButton:pressed {
    background-color: rgb(224, 0, 0);

    QPushButton:checked {
    background-color: rgb(0, 0, 224);

    you will have a push button that change color when checked
    (note colors are random and butt ugly :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.