Important: Please read the Qt Code of Conduct -

Too many connections to slots?

  • I just got done making a small color choosing widget. I added some print statements into the slots to see of often they were being called. I noticed that even if I only move one slider/spinner, the same slot may be called more than once. This may be beucase I'm using the setValue() funciton which should emit a signal.

    The code is below (in Python and PyQt), but I'm wondering if this is fine, or if I should try to find some way to make sure a slot is only called once during "one," event.

    The code is too big to post, "So here's a link":

  • You definitely have a circular signal/slot connection going on there - setValue() on the spin box emits valueChanged() which invokes setValue() on the slider which emits a valueChanged() which invokes setValue() on the spin box, and so on.

    However, functions like "setValue()" on widgets have an emit policy which is usually only to emit if the value is changed.

    So if we setValue(X) our spinbox (hence emitting a signal) which calls setValue(X) on our slider (which emits a signal) which then calls setValue(X) on our spinbox, we don't get a signal from this final setValue() as the value has not changed - it has been set from X to X. So you might be calling the setValue slot a couple of times whenever you change a value.

    To summarise, then, setValue only emits a valueChanged if the new value is different from the previous value, so your code is fine.

  • Okay, I'm trying to avoid an ifinite loop or something along those lines. I think it is possible to make it so that only one slot/signal get called/sent, but it looks like this is fine as is.

    Thanks for the reasurance.

Log in to reply