[Solved] Toggle button by label only

  • I have a checkable QPushButton which I use to toggle a function as indicated by its button text. However, in addition, the button raises and lowers with each toggle--I want the button to appear unpressed in both states.

  • Lifetime Qt Champion


    Do you mean that you want your button to act as if he was not checkable ?

  • I don't think so. Let me rephrase: I want my button to be able to toggle between two actions, but the only indication of this should be its changing text.

  • Moderators

    connect the button to a slot where you toggle the text back and forth.
    You can set and check the state e.g. by using dynamic properties when you do not want to subclass QPushButton.

    But i would recomment subclassing QPushButton and manage the satet with a member variable.

  • I'd still like the button to be checkable though, just without the sustained pressed effect. Is there perhaps a way to "release" the button without untoggling it?

  • Moderators

    may i ask why you don't want this?

    It's possible by:
    void MyButton::mousePressEvent(QMouseEvent* event)
    this->setChecked( ! this->isChecked() ); //toggle checked state
    //don't call base-class implementation to avoid push effect of button;
    //not calling the base class implementation may have other side effects you may have to take care of yourself, but i can't think of any right now.

  • Hi,

    try this by designer or code:

    1. set the button as flat

    2. set stylesheet background and ::focus the same color: for non focus you should provide in style sheet also a border


    border:0px solid rgba(0,0,0,0);


    1. set it as checkable and checked as the first state

    2. change text:


    hope it helps!


  • Raven-worx:

    I tried your second suggestion first - it worked for appearances, but as you predicted, it was too buggy to bother fixing.

    I had originally wanted to use a checkable button for the simplicity of being able to use pushButton->isChecked(), but based on your first suggestion I switched to using pushButton->text() == "Status 1", which wasn't so hard either.

    Thank you!

Log in to reply

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