Solved 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 usingmyCheckBox.stateChanged.connect(self.update_db)
to triggerupdate_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 callmyCheckBox.setChecked()
The problem is that when I call
setChecked()
it triggersstateChanged()
which in turn triggersupdate_db()
which is an unnecessary call.Is it possible to call
setChecked()
(orsetCheckState()
) without triggeringstateChanged()
?
Or is it possible to identify ifstateChanged()
was triggered by the user action or by the code?UPDATE:
A possible solution would be to connect toclicked()
instead - it doesn't triggerstateChanged()
. Is this the only solution? -
Hi,
You can use QSignalBlocker.
-
@SGaist Thanks! For now I switched to
clicked()
which seems to be fine, but checkedQSignalBlocker
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!