How can I check signal / slot connection at compile time?

Checking the connection to the Qt signal connector at runtime is an excitement for me. I should be able to run a static test of connect statements.

Is there such a tool?

+8
c ++ qt signals-slots static-analysis
source share
6 answers

Using QT5, you can use the following syntax, which is statically checked at compile time:

connect(sender, &Sender::signalMethod, receiver, &Receiver::slotMethod); 
+3
source share

Is there such a tool?

It would be nice if such an instrument existed, but, unfortunately, it does not exist, due to the fact that the signal / slot mechanism is implemented in qt. In addition, for this reason it is not possible to statically check whether a signal is inserted in the slot.

If qt used something like an alarm / slots, that would be possible.

+3
source share

You might consider creating a GCC plugin in C or MELT extensions, MELT is a domain-specific language that easily encodes GCC extensions.With plugins or MELT extensions, you can analyze internal views (in particular Tree-s, representing class declarations and functions C ++) and make a special tool for this.

However, expanding GCC requires an understanding of its rather complex internal presentation and will require more than a week of effort for a person who does not know the internal components of GCC.

Perhaps this effort is not worth it if your Qt application is really great. If you think you are working with MELT, I would be happy to help you.

+2
source share

I used something like this in my code:

  #define CONNECT_OR_DIE(source, signal, receiver, slot,connection_type) \ if(!connect(source, signal, receiver, slot,connection_type)) \ qt_assert_x(Q_FUNC_INFO, "CONNECT failed!!", __FILE__, __LINE__); 

I used it instead of just calling connect (). Does this help you?

+2
source share

You cannot verify this at compile time, but if you run the program in debug mode inside Qt Creator, it will print a useful diagnostic message in the Application Ouptut panel if the connect call fails. See my answer here .

+1
source share

To work with a large Qt4 base, I wrote a test such as the plugin for the clang static analyzer

See: http://reviews.llvm.org/D14592

Example of test coverage:

  connect(&send, SIGNAL(f2(int, double*)), &recv, SLOT(bugaga(int, double*))); // expected-warning{{Can not find such slot}} 
+1
source share

All Articles