How can I check if QPushButton is clicked - c++

I want to use an if-else statement to check if QPushButton is clicked or not.
How can I do this?.

The question does not make sense. Being clicked is not a state that you can check; clicking a button is an event. It is important to distinguish between states and events.
You handle a button click event by connecting a slot to the signal QAbstractButton::clicked().
Maybe you mean "How do I check if a button is down?". Being down is a state; you check that state using the method QAbstractButton::isDown().

QAbstractButton, QPushButton's parent class, has a checked property (setChecked/isChecked) if you're trying to determine if the button is depressed with the checkable property set to true.

Related

Qt Radio Button only call SLOT() when button is actually checked

I've got a group of radio buttons. These are all connected to the same SLOT(updateImage()).
Here's how they are set up:
connect(m_radio1, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio2, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio3, SIGNAL(toggled(bool)), this, SLOT(updateImage());
When changing the radio button from one to the next, this SLOT() is called twice, once for de-selecting the previously selected radio button, and once for selecting the clicked radio button.
I was wondering, is there a way to modify my SLOT() to only occur in one of these cases, when the clicked item becomes checked?
Thanks
You are asking to connect to the toggled signal - you are called whenever the object is toggled. What's surprising?
You could connect to the "clicked" signal which provides a "checked" argument you can test.
Btw; if using a modern Qt version, you should ditch the SIGNAL and SLOTS macros and instead use the new connect() syntax that's checked at compile time.
Since your signal have a bool parameter.
toggled(bool)
you could add that parameter to your SLOT method.Then check if the parameter is checked.
(As a complement: How to do so in Python?) Assume you have a radio button radio_button and you would like to connect it to slot _my_slot while passing args as the arguments, such that radio_button only send a signal once it is selected, then you need to add the following:
radio_button.clicked.connect(lambda: _my_slot(args))

What is the difference between QCheckBox::toggled() and QCheckBox::clicked()?

Is there any practical difference between the QCheckBox::toggled(bool) and the QCheckBox::clicked(bool) signals? Both have the same signature, does it matter to which one I connect?
The toggled signal is emitted every time the check state of the checkbox changes, even if it changes through code, while the clicked signal is emitted only when the user interacts with the checkbox, eg:
ui->checkbox->setChecked(true); // toggled() will be emitted, but not clicked()
QCheckBox Inherit both toggled and clicked.
void QAbstractButton::clicked ( bool checked = false ) [signal]
This signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().
If the button is checkable, checked is true if the button is checked, or false if the button is unchecked.
void QAbstractButton::toggled ( bool checked ) [signal]
This signal is emitted whenever a checkable button changes its state. checked is true if the button is checked, or false if the button is unchecked.
This may be the result of a user action, click() slot activation, or because setChecked() was called.
The states of buttons in exclusive button groups are updated before this signal is emitted. This means that slots can act on either the "off" signal or the "on" signal emitted by the buttons in the group whose states have changed.
For example, a slot that reacts to signals emitted by newly checked buttons but which ignores signals from buttons that have been unchecked can be implemented using the following pattern:
void MyWidget::reactToToggle(bool checked)
{
if (checked) {
// Examine the new button states.
...
}
}
http://qt-project.org/doc/qt-4.8/qcheckbox.html
QCheckBox::toggled(bool)
Emitted when the check box changes its state, whether that's through clicking it or using setChecked or toggle, etc.
QCheckBox::clicked(bool)
Emitted when the check box is clicked. That is, when the user clicks and releases on check box. Also occurs when the shortcut key is typed or click is used. Check box doesn't necessarily have to be checkable for this to be emitted.
If you're listening for when the state of the check box is changing, use toggled. If you're listening for when the user clicks the check box, regardless of whether that changes state or not, use clicked.

Possible bug in QPushbutton?

I want to implement something similar to Ribbon UI found in MS Office 2007.
I am using QPushbutton's along with QStacked Widget. When a QPushbutton is pressed corresponding widget from Stacked Widget is displayed. I want to implement in such way that when a PushButton is pressed down we should not be able to press it again except if some other QPushButton is pressed.
So for the clicked() SLOT of my QPushButton I am calling this: button->setDown(true); in my 1st line.
According to the documentation:
If this property is true, the button is pressed down. The signals
pressed() and clicked() are not emitted if you set this property to
true.
So when I click it at run time the button is shown as pressed down. Thats good! However the SIGNAL's are still emitted i.e. pressed() & clicked() are emitted.
I have tried for the same property using different combinations of SIGNAL's & SLOT's. However its just the same. I am using Qt 4.8.1.
What is going wrong here?
Thank You.
If you want to set the button visual appearance to pressed you can use the setDown() function. The documentation isn't very clear but:
If this property is true, the button is pressed down. The signals pressed() and clicked() are not emitted if you set this property to true.
It doesn't mean that that signals won't be emitted while the button is pressed but that they won't be emitted when you call setDown() (after all the button is enabled and a 2nd click may simply toggle its state).
What you can do is to check if the button is pressed (isDown()) inside your handler for clicked(). As alternative you may change the button to be checkable (with setCheckable())): 2nd click on the button will "close" it (if it's the behavior you need).
set all the buttons to checkable and then read up on
http://qt-project.org/doc/qt-4.8/qbuttongroup.html

Qt4 QTableView and checkbox click event

I have created QTableView and add an QStandardItem checkbox into with this code:
....
QStandardItem *chkItem = new QStandardItem(true);
chkItem->setCheckable(true);
chkItem->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
tblModel->appendRow(chkItem);
...
where tblModel is QStandardItemModel.
Now i wonder how can i catch the checkbox changed state event?
I have tryed catch the table click event but this is triggered every times user click on a cell (i have no idea to know whether the checkbox state has changed or not). Please give me a way to solve this problem.
Try QStandardItem::checkState() to retrieve the Qt::CheckState of the checkbox. If you need to know, if the state changed, then save the old state somewhere and check if the state changed on the click event.

QWidgetAction : how to make the menu disappear after the user completes his input

In my QMenuBar, I have several menus.
One of those menus has a QWidgetAction in it.
It shows up fine, but the problem is that once the user completes his input, I want the menu to disappear (as is the normal behavior for a classical QAction).
However, I am not sure on how to do that. In my QWidgetAction, there is a button the user presses when he is done; I can therefore bind to this button's clicked() signal.
In the slot, I tried to setFocus() an element outside the menu but the menu still doesn't disappear.
How to tell the menu to close itself when my users finish interacting with the QWidgetAction?
Thanks
QMenu inherits QWidget, so calling yourMenu->hide() should do the work.
Hope this helps.