Auto-generate a map of all signal-slot connections
-
Hi All,
I have made a Qt program that uses signals and slots to exchange messages between the different modules.
Now I have manually made a diagram that shows the relation between the different modules like this:
This is the code of the diagram, it can be rendered with graphwiz:
digraph structure { node [shape=record] rankdir=LR ObjectA [label = "ObjectA | <slotA> slotA | <slotB> slotB | <slotC> slotC | <sigA>sigA" ] ObjectB [label = "ObjectB | <slotA> slotA | <slotB> slotB | <slotC> slotC | <sigA>sigA" ] ObjectC [label = "ObjectC | <slotA> slotA | <slotB> slotB | <slotC> slotC | <sigA>sigA" ] ObjectA:sigA -> ObjectB:slotC ObjectB:sigA -> ObjectC:slotC ObjectC:sigA -> ObjectA:slotA }
As far as I can tell, it should be possible to extract the above diagram from the source of the Qt program, the signals and slots can be extracted from the class declarations, and the lines in between them can be gathered from the connect () statements in the program.
Does anybody know a tool that can scan the sourcecode of a Qt program, and then generate such a diagram?
-
It doesn't create a graph like your example, but Gammaray will probably give you the most info about signal/slot connections. It displays info from a running Qt-based application, since signal/slot connections are often dynamically created and disconnected during the lifetime of a running application.
-
@cdwijs said in Auto-generate a map of all signal-slot connections:
As far as I can tell, it should be possible to extract the above diagram from the source of the Qt program
Not really, no.
the signals and slots can be extracted from the class declarations
That's true, although keep in mind that C++ is pretty complex language. You can obscure stuff behind macros and templates. You'd have to pretty much use a compiler to get an accurate listing or at least mimic what Qt's moc tool does. There's also stuff like QUiLoader which create objects dynamically at runtime from text files. You can't track that just from source.
You could use a QMetaObject to inspect a class, but that's a runtime inspection, not a static code analysis.
(...) and the lines in between them can be gathered from the connect () statements in the program.
Connections are made at runtime. You can't extract them from source code. They can be disconnected and reconnected multiple times in response to events that may or may not happen and objects are created and destroyed during runtime too.
Also, imagine a function like this:void doConnection(QObject* src, const char* src_signal, QObject* dst, const char* dst_slot) { QObject::connect(src, src_signal, dst, dst_slot); }
and function parameters are runtime user input. Analyzing this source tells you nothing about the objects or signals/slots.
The best you could do is get a runtime snapshot of app state at a specific point in time and objects and connections existing at that time. That's what tools like GammaRay mentioned by @mchinand do.