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

Can I Intercept client calls to connect a slot to my object's signal?



  • I'm trying to figure out how I can intercept client calls to connect() to a signal exposed by my object. Emitting my signal requires some significant CPU work to be done by my object and only wish that to happen while some client has actually connected a slot to it so as to avoid wasting CPU otherwise.

    Is there a way I can make my object intercept client's connecting to / disconnecting from my signal?

    I've gone over the documentation and tried to step through the actual connect() code but I don't see a good spot.

    Thanks,

    -Joe



  • Hi! int QObject::receivers(const char *signal) const is exactly what you're looking for. See: http://doc.qt.io/qt-5/qobject.html#receivers



  • Hi Wieland. Thanks for answering.

    Unfortunately, unless I'm missing something, that function doesn't seem to be what I want. It lets me check the number of receivers but doesn't notify me when someone subscribes, is that right?

    I need to know right at the point someone connects to my signal that the connection has occurred. I need to be able to intercept the connect() calls themselves in some way or at least be told about it right after.

    (Maybe there is some virtual function I could override...?).

    When the first client connects, I start my expensive logic that periodically sends out signals.



  • I think connectNotify is what you want.



  • Perfect, that is it! I don't know how I could have missed that. I thought I'd scanned those docs up and down.

    Thank you


Log in to reply