Qt World Summit: Register Today!

QCheckBox change state without triggering stateChanged

  • I'm stuck with the following problem.
    I have a QCheckBox and a list of items in a table. I'm recording checked/unchecked state of each item to the database when user selects and item and checks/unchecks the checkbox.
    I'm using myCheckBox.stateChanged.connect(self.update_db) to trigger update_db() that updates the item state in the database.
    I also need to update the state of the checkbox to show the current state of the item when user selects the item in the table.
    To do this I call myCheckBox.setChecked()

    The problem is that when I call setChecked() it triggers stateChanged() which in turn triggers update_db() which is an unnecessary call.

    Is it possible to call setChecked() (or setCheckState()) without triggering stateChanged()?
    Or is it possible to identify if stateChanged() was triggered by the user action or by the code?

    A possible solution would be to connect to clicked() instead - it doesn't trigger stateChanged(). Is this the only solution?

  • Lifetime Qt Champion


    You can use QSignalBlocker.

  • @SGaist Thanks! For now I switched to clicked() which seems to be fine, but checked QSignalBlocker and it works too, good to know it exists.
    I'm new to Qt and it's really impressive how thought out to the details it is! And what's more important there are gurus who share their knowledge and respond quicker than tech support of most of the software dev companies :-) Thanks again!

Log in to reply