Search code examples
qtpyqtqt4pyqt4qcheckbox

QCheckBox: how to differentiate between user-induced changes to state and those made programmatically?


Do I miss something or there is really no (ready / built-in) way to programmatically change the state of a QCheckBox without emitting the "void stateChanged ( int state )" signal?

The above-mentioned signal is emitted regardless of whether "void setCheckState ( Qt::CheckState state )" was called or the user changed the state via the ui, and there is no "stateEdited" signal like with the QLineEdit.

So, if there is no ready way to differentiate between programmatic and user-induced changes to the state of the QCheckBox, and the only options are subclassing / adding the "stateEdited" signal or fiddling with "void QObject::blockSignals( bool block )", why does this have to be so, i.e., is it an (some sort of) inconsistency (in Qt)?


Solution

  • If you only need to be informed of user input, listen to

    QAbstractButton::clicked( bool checked );
    

    Otherwise connect to

    QAbstractButton::toggled( bool checked );
    

    or

    QCheckBox::stateChanged( int state);