Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get Background Color when QPushButton is Pressed



  • Hi!

    How can I get QPushButton's pressed background color?

            if (isDown())
                _BGColor = <pressed back color>; //should get the pressed bg color
            else
                _BGColor = palette().color(QPalette::Background); //gets the idle/normal bg color , working fine
    

    I don't know if I can get my expected result when doing the above code.

    Thanks in advance!!!



  • It is not clear what you want to do.

    If you have a few buttons with different background color and connected signal clicked to slo6t lets day onClick();

    and within such slot you want to find color of the button background which was clicked,
    first thing you need to find - which button was clicked.

    There are a few ways to find it.
    Simplest (not the best) is to call sender() within a slot.
    Cast ( use qobject_cast) to a QPushButton* ( or whatever your buttons are ),
    check its background if it is null.

    Why this way is not the best? It makes an assumption that slot is only called as a result of button click,
    which can't be guaranteed.

    It is better to use QSignalMapper.