9

Checking the Qt signal slot connect calls at runtime is a worry for me. I should be possible to run a static check of the connect statements.

Does such a tool exist?

Phil Hannent
  • 12,047
  • 17
  • 71
  • 118

6 Answers6

6

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

connect(sender, &Sender::signalMethod,  receiver, &Receiver::slotMethod);
Pierluigi
  • 2,212
  • 1
  • 25
  • 39
3

Does such a tool exist?

It would be nice if such tool would existed, but unfortunately it doesn't exist, because of the way signal/slot mechanism is implemented in qt. Also, for that reason it is not possible to statically check whether signal fit into the slot.

If qt used something like boost's signal/slots it would be possible.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • Could you expand on why it would not be possible? With a complete source tree I don't see why I shouldn't be able to compare signals and slots. Sure I need to look in more than one file at a time but it should still be possible. – Phil Hannent Jan 03 '12 at 13:47
  • @PhilHannent [This page](http://developer.qt.nokia.com/doc/qt-4.8/signalsandslots.html) describes QT's signals/slots. It is possible up to some limits (for example, wrong signatures in SIGNAL and SLOT). Sure, you can write your own tool that does it, but the current implementation of QT doesn't do it. – BЈовић Jan 03 '12 at 14:20
2

You might consider making a GCC plugin in C, or a MELT extension, MELT is a domain specific language to easily code GCC extensions. With plugins or MELT extensions, you could analyse the internal representations (notably Tree-s representing C++ class & functions declarations) and make a specific tool for that.

However, extending GCC requires to understand its quite complex internal representation, and would require more than a week of efforts for a person not knowing GCC internals.

Perhaps such an effort is not worthwhile, unless your Qt application is really big. If you consider working with MELT on that, I would be delighted to help you.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

I've 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 the simple call to connect(). Does it help you??

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
1

To work with large Qt4 codebase I wrote such checker as plugin for clang static analyzer

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

example of it test coverage:

    connect(&send, SIGNAL(f2(int, double*)), &recv, SLOT(bugaga(int, double*))); // expected-warning{{Can not find such slot}}
fghj
  • 8,898
  • 4
  • 28
  • 56
1

You can't check this at compile time, but if you run the program in debug mode inside Qt Creator, it will print a helpful diagnostic message in the Application Ouptut pane if a connect call fails. See my answer here.

Community
  • 1
  • 1
TonyK
  • 16,761
  • 4
  • 37
  • 72
  • I didn't mean literally at compile time, just as part of the tools which are run (qmake, nmake/make, cppcheck, etc). I have the application messages being output, however if you have dynamically created objects its a lot harder to test because you have to make sure you create at least one of those objects each time you to acceptance testing. – Phil Hannent Jan 03 '12 at 13:45