Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[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.
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.
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?
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.
NicuPopescu last edited by
try this by designer or code:
set the button as flat
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);
set it as checkable and checked as the first state
hope it helps!
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.