[Solved] Change behaviour of QPushButton?
-
Hi,
I have a QPushButton in the main window of my application.
What I want is that, when the user clicks it, it should not be restored back to its raised position. I want that the pushbutton should remain pressed visually. How do I do that?
& I want to again restore the state of the button to raised in a function dependin upon some variables.
How do I do this?
Also I am using a SLOT which is triggered whenever the clicked() SIGNAL is emitted by the Push Button. So what I want is that when the button is in the pressed down state I dont want the SIGNAL clicked() to be emitted. Or atleast is there some way by which I can compare value of a property so as to bypass the code in the SLOT.
Thank You. -
bq. If you need toggle behavior (see setCheckable()) or a button that auto-repeats the activation signal when being pushed down like the arrows in a scroll bar (see setAutoRepeat()), a command button is probably not what you want. When in doubt, use a tool button.
I think what you want is called "toggle behavior". In other words, you want a QToolButton instead of a QPushButton, which is a "command" button, meaning specific things about what it means to the user and how it behaves visually (goes down but comes right back up.) The documentation for QPushButton seems easy to understand, but it is full of subtle meaning, for example, what "toggle" means.
-
"toggleness" or whatever you call it is nothing unusual for QPushButton too. Just set the "checkable" property and connect to toggled(bool) instead of clicked().
An example:
@
//connect stuff
ui->button->setCheckable( true ); //or corresponding checkbox in the designer
connect(ui->button, &QPushButton::toggled, this, &MainWindow::someSlot);//and then...
void MainWindow::someSlot(bool checked)
{
if(checked) //button is "down"
{
//do stuff...
ui->button->setChecked(false); // "raise" the button
}
else //button is "up"
{
//do other stuff or ignore...
}
}
@ -
Yes, one can "roll your own" behavior by using properties of the superclass QAbstractButton. But then you should probably worry about user friendliness, or user interface guidelines.
Back up in the design process, and ask whether the button is "starting an action" (QPushButton, which is a command) or "changing a state" (QToolButton.) Then possibly the original question becomes moot, since the QToolButton might behave as desired (unless you are really designing a novel user interface technique.)
-
Yes, I'm wrong, I didn't read far enough about QToolButton. Both can be setCheckable and QToolButton doesn't default to checkable, it also can be used for "commands or options", where an option has a state.
And the difference between a command and a state is fuzzy. A VCR button (play, pause, fastforward) is commanding a process (the player) to start, but also putting that process in a state (playing).
-
Hi @Krzysztof Kawa,
Thank You man!!!
That solution worked :-)
Previously I was trying everything with setChecked() & the various SIGNAL's of QPushButton but could not get the desired behaviour.
In your code I saw setCheckable() & then I tried again & it worked.
I think it was foolish of me not to find that previously.
Thank You Andre & bootchk as well :-)