Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Qt signals, slots and connections

Is there a way to see which signals are fired, and if there is a slot connected to them? Ideally, we'd like to see all signals, not just those of a particular class or method; e.g. QSignalSpy only allows us to track specific signals of specific instances.

In our application, we've seen performance problems because of a signal being emitted twice from different components. In the end, it turned out that there was a second instance of a class that should have only been there once. Knowing which signals are emitted exactly helps in debugging this.

Signals are called via QMetaObject::invoke*, I was hoping to find something there to hook into, but I found nothing obvious.

like image 819
Ivo Avatar asked Sep 20 '10 10:09

Ivo


People also ask

How signals and slots work in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

Are Qt signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.


2 Answers

(disclaimer, I work for KDAB) : KDAB's GammaRay tool can show you objects and connections at runtime, without requiring any source changes. It inspects the meta-object tables and does some code-injection hooks to make this work.

like image 127
James Turner Avatar answered Sep 20 '22 19:09

James Turner


QSignalSpy could help you.

From docs,

The QSignalSpy class enables introspection of signal emission.QSignalSpy can connect to any signal of any object and records its emission.

The docs has examples too..

like image 37
liaK Avatar answered Sep 17 '22 19:09

liaK