Prevent Firing Signals in Qt

前端 未结 8 959
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 14:07

We have a QCheckBox object, when user checks it or removes check we want to call a function so we connect our function to stateChanged ( int state )

8条回答
  •  攒了一身酷
    2020-12-14 14:41

    In QObject derived classes, you can call blockSignals(bool) to prevent the object from emitting signals. So for example:

    void customChangeState(bool checked)
    {
        blockSignals(true);
        ui->checkBox->setCheckState(Qt::Checked);
        // other work
        blockSignals(false);
    }
    

    The above method would change the check state without clicked, stateChanged, or any other signals being emitted.

提交回复
热议问题