Determine signals connected to a given slot in Qt

Serafina Brocious picture Serafina Brocious · May 3, 2010 · Viewed 29.2k times · Source

I've injected myself into a Qt application, and I'm attempting to figure out what signals a given slot is connected to, but can't find any information on doing this. Is there a mechanism for doing this out of the box? If so, is this exposed to QtScript? (If not, I can wrap it easily enough.)

If there is no such mechanism, what would be the best way to add it? I cannot manipulate the existing application outside of simple hooking, but I could hook QObject::connect and store the connections myself, just not sure if that's the best way to go about it.

Answer

duncan picture duncan · May 3, 2010

I think Qt stores the slots a given signal is connected to, so that when you emit it all receivers are called, therefore you can access the list of receivers:

For debugging purposes, you have:

void QObject::dumpObjectInfo ()

Dumps information about signal connections, etc. for this object to the debug output.

This function is useful for debugging, but does nothing if the library has been compiled in release mode (i.e. without debugging information).

And the list of slots a signal is connected:

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

Returns the number of receivers connected to the signal.

The metaObject() gives you the QMetaMethod for the slot, but it has no information about its connections.

However, if you know the objects, you can go over all the the signals (using the meta object, testing the method type for signal) and build a reverse index with the slots receivers() gives you.