views:

239

answers:

2

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.

+5  A: 

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.

duncan
Thanks for the info. Is there any facility to do this without knowing the objects? That's really the issue I'm running into at the moment.
Cody Brocious
A: 

After digging around in the Qt code base and documentation (I got lots of helpful tips from here and elsewhere), I ended up settling on hooking QObject::connect (the static overload). Why? Well, the other solutions require you to know what objects are providing the signals, dig into private fields, or have to have a debug build of Qt. In the end, hooking QObject::connect gives you everything connected in the application, and you can trivially map back to the slots.

Cody Brocious