如何在编译期间检查信号/插槽连接?
在运行时检查 Qt 信号槽连接调用对我来说是一个担心。我应该可以对连接语句进行静态检查。
存在这样的工具吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为了使用大型 Qt4 代码库,我编写了这样的检查器作为插件
对于 clang 静态分析器
请参阅:
http://reviews.llvm.org/D14592
测试覆盖率示例:
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:
使用 QT5,您可以使用以下语法,该语法在编译时进行静态检查:
Using QT5 you can use the following syntax which is statically checked at compile time:
如果存在这样的工具就好了,但不幸的是,由于 qt 中信号/槽机制的实现方式,它不存在。此外,由于这个原因,不可能静态地检查信号是否适合插槽。
如果 qt 使用类似 boost 的信号/槽之类的东西,那是可能的。
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.
您可能会考虑用 C 语言制作一个 GCC 插件,或者一个 MELT 扩展,MELT 是一种可以轻松编码的领域特定语言海湾合作委员会扩展。通过插件或 MELT 扩展,您可以分析内部表示(特别是代表 C++ 类和函数声明的 Tree-s)并为此制作一个特定的工具。
然而,扩展 GCC 需要了解其相当复杂的内部表示,对于不了解 GCC 内部结构的人来说,需要花费一周以上的努力。
也许这样的努力是不值得的,除非您的 Qt 应用程序真的很大。如果您考虑与 MELT 合作解决此问题,我将很高兴为您提供帮助。
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.
我在我的代码中使用了类似的东西:
我使用它而不是对 connect() 的简单调用。对你有帮助吗??
I've used something like this in my code :
I used it instead of the simple call to connect(). Does it help you??
您无法在编译时检查这一点,但如果您在 Qt Creator 中以调试模式运行程序,如果
connect
调用失败,它将在“应用程序输出”窗格中打印一条有用的诊断消息。请参阅我的回答。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.