简体   繁体   中英

How can I do a check of the signal/slot connect's during compilation?

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?

使用QT5,您可以使用以下语法,该语法在编译时进行静态检查:

connect(sender, &Sender::signalMethod,  receiver, &Receiver::slotMethod);

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.

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.

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??

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}}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM