Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Calling slot only once, even if multiple signals request it



  • Hi,

    I have the following scenario:
    I have settings widgets A, B and C and and image rendering widget I. Then I have the following signal to slot connections:
    A.changed() -> I.render();
    B.changed() -> I.render();
    C.changed() -> I.render();

    Now the problem is that A also changes values of B and C. Thus, changing A results in 3 I.render() calls (one from A, one from B and one from C). How can I queue the call to I.render() and keep only one request in the queue() ? The QWidget update() function has a similar behavior AFAIK.

    I could possibly do it using QTimer, but is there a better way?

    Regards,

    Eddy



  • Yeah, that's how I would do it: Connect ABC.changed() not to I.render() directly, but instead to some other slot, like I.update(). This new slot will do nothing but enable a QTimer (in "single shot" mode!) - unless the timer is already enabled. Finally, your QTimer is connected to I.render().

    Alternatively, you could work with blockSignals(), in order to temporarily disable the signals of B and C when A fires. But that will introduce a lot of dependencies (A needs to take care of B and C now, for example).



  • Is that the common way to do it then?

    Best,

    Eddy


  • Lifetime Qt Champion

    Hi,

    The QTimer is a common technique. One variant that you can use is to restart the timer rather than check if it's enabled so render will only be called after no changes happened after a while.


Log in to reply