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

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.

Tags:

C++

Qt