[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.
-
Hi,
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.
this->update();
}
@ -
Hi,
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
i.e.
@#pushButtonTv
{
border:0px solid rgba(0,0,0,0);
background-color:rgba(255,255,255,255);
}#pushButtonTv:focus
{
background-color:rgba(255,255,255,255);
}@-
set it as checkable and checked as the first state
-
change text:
@if(ui->pushButton->isChecked())
ui->pushButton->setText("Checked");
else
ui->pushButton->setText("Unchecked");@hope it helps!
Cheers!
-
-
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!