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

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.

Related

QPushButton setDown on click

When a QPushButton is clicked, I want it to remain pressed down until clicked again.
void MainWindow::itemClicked(){
QPushButton *clickedItem = qobject_cast<QPushButton *>(sender());
qDebug() << clickedItem->isDown();
if(!clickedItem->isDown())
clickedItem->setDown(true);
else
clickedItem->setDown(false);
}
This doesn't seem to work. It will cause the button to be pressed down indefinitely.
clickedItem->isDown() is always false.
isDown always returns false because you are checking it in a slot connected to the clicked signal. The clicked signal is emited when you press down the push button and release it. So every time the button is pressed and released the clicked signal is emited.
setCheckable() would work for you. It will make the button toggle. So when youu click, it'll stay in down state until you click it again.
It should work out of the box using QAbstractButton::setCheckable(bool).
When set to true it should act the way you want it to act.

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

How can I check if QPushButton is clicked

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.

QToolButton and emiting signals

What signal is emmitted if I click on QToolButton's arrow part of a button? The clicked signal is emitted wherever I click on it but I need to detect only when the specified part is clicked on.
What you have to do is to intercept menu's aboutToShow signal and connect this signal with id est, reinitialize_menu_ slot, and in this slot you can do necessary checks and adjustments.
Good luck.

How thread-safe and latency-safe is QApplication::mouseButtons?

When you get get a mouse signal from a model into your slot, the argument passed is a QModelIndex.
QModelIndex does not tell you what button is pressed. So, we can resort to QApplication::mouseButtons. But QApplication::mouseButtons is the current button press, not when model experienced the click.
My thought experiment is saying that, after the right-button is pressed, the underlying view sends the signal to my widget, but just before my widget's slot received the signal, a spurious left-click occurred. So calling QApplication::mouseButtons on receipt of QModelIndex would wrongly associate the row being click with the left mouse button rather than the right button. How possible is this scenario?
When you look at Qt and even QML, it takes a lot of code acrobatics to achieve proper mouse button info on receipt of a QModelIndex. Is it a policy that nokia is striving to promote mouse button agnosticism?
I don't think this is a very possible scenario but it may happen.
A "simple" way to be sure about which button was clicked is to subclass QTableView (or the view you are using and reimplement the mouseReleaseEvent.
void mouseReleaseEvent(QMouseEvent * event)
{
// store the button that was clicked
mButton = event->button();
// Now call the parent's event
QTableView::mouseReleaseEvent(event);
}
By default the mouseReleaseEvent emits the clicked signal if an item of the view is pressed
If a user presses the mouse inside your widget and then drags the
mouse to another location before releasing the mouse button, your
widget receives the release event. The function will emit the
clicked() signal if an item was being pressed.
The trick is to catch the clicked signal in your derived class and emit a new signal which except the model index will contain the button as well.
// Define your new signal in the header
signals:
void clicked(QModelIndex, Qt::MouseButton);
// and a slot that will emit it
private slots:
void clickedSlot(QModelIndex);
// In the constructor of your derived class connect the default clicked with a slot
connect(this, SIGNAL(clicked(QModelIndex), this, SLOT(clickedSlot(QModelIndex)));
// Now the slot just emits the new clicked signal with the button that was pressed
void clickedSlot(QModelIndex i)
{
emit clicked(i, mButton);
}
You can do something similar with the mousePressEvent if you need the pressed signal as well.