5

Using Qt 5.11. By overriding QObject::connectNotify, I can get notified if a slot was connected to my signal:

    void connectNotify(const QMetaMethod &signal) override {
        if (signal == QMetaMethod::fromSignal(&PromiseBase::resolved()) && isResolved()) {
            // Here, I want to call the slot immediately.
        }
    }

When this happens, I want to immediately call the slot – and just that slot, not all other slots that might be connected to the signal. How can I do this? The Qt framework gives me the signal, but not the slot.

Context: I'm working on a Promise class similar to JavaScript promises. The expectation is that connecting a slot to the resolved signal will always call the slot exactly once, regardless of whether the promise has been resolved or is still pending.

(I'm writing my own Promise rather than using QFuture, because there is no documented and supported way to create your own QFuture objects, and QFuture has no support for reporting errors.)

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Side note: calling the slot from within the connect() call might be surprising behavior to some Qt developers who don’t expect their slot-method to be called while they are still in the middle of setting things up. You might want to use ‘QTimer::singleShot(0, ...)’ or similar to defer the call until the next iteration of the Qt event loop instead. – Jeremy Friesner Feb 03 '19 at 19:31

2 Answers2

0

As explained in this answer it is not an easy task to do. Depending on your situation, some sort of hook mechanism could be sufficient.

ch200c
  • 46
  • 4
-1

You have the function

int QObject::receivers ( const char * signal ) const [protected]

Returns the number of receivers connected to the signal.

doudouremi
  • 186
  • 1
  • 6