4

I am wondering if there is anyway to disable (setCheckState) on a check box (QCheckBox) in pyqt "WITHOUT" triggering an event? So it is simply a display change with the checkbox now unticked but not triggering the method I have it connected to. Thanks for any help.

Would love a basic example if possible.

fuesika
  • 3,280
  • 6
  • 26
  • 34
Nick van Diem
  • 95
  • 1
  • 7

2 Answers2

3

You can simply block signals just before disabling the QCheckBox, and re-enabling them just after. Assuming chk is your QCheckBox object just do:

chk.blockSignals()
# then you change the checkbox as you want
chk.unblockSignals()
mguijarr
  • 7,641
  • 6
  • 45
  • 72
  • The big downside of `blockSignals()` is that it blocks *all* signals for the widget - including the ones Qt connects to internally. This can sometimes make it a bit of bug trap, due to unexpected side-effects. – ekhumoro Jun 05 '15 at 15:40
  • @ekhumoro ...good to know. It's fine for my purposes because I literally enable directly after I uncheck. However, would be keen to hear your alternative? It seems like a scenario that would be often useful ...manipulate the ui without triggering events. – Nick van Diem Jun 08 '15 at 21:58
  • @NickvanDiem. Although my comment is broadly true, at the time I made it I was certain I had a simpler alternative. But it seems my memory was playing tricks on me, and that turns out not to be the case. The only real alternative I can think of now is to disconnect and reconnect the signal - which is certainly safer, but not as simple. – ekhumoro Jun 09 '15 at 00:23
  • @ekhumoro ...no problems. Thanks very much for pondering on it :) – Nick van Diem Jun 11 '15 at 09:43
2

The answer of @mguijarr says it all, but apparently they changed the function a bit.

In QT 5.14.0 it is:

chk.blockSignals(True)
chk.blockSignals(False)
# same syntax for c++ and py at this place

docs for python

docs for c++

(Maybe this would better be a comment, but I need more rep for comments)

C. Mayer
  • 383
  • 3
  • 10
  • 1
    In which part of the docs does it indicate that unblockSignals can be used? I do not see that anything has changed regarding this method. – eyllanesc Jan 14 '20 at 19:24
  • it was already too late yesterday... I wanted to correct the outdated information but just repeated the second statement. I fixed it. – C. Mayer Jan 15 '20 at 20:06