如何在编译期间检查信号/插槽连接?

发布于 2024-12-25 03:05:38 字数 69 浏览 3 评论 0原文

在运行时检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

︶葆Ⅱㄣ 2025-01-01 03:05:39

为了使用大型 Qt4 代码库,我编写了这样的检查器作为插件
对于 clang 静态分析器

请参阅:
http://reviews.llvm.org/D14592

测试覆盖率示例:

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

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}}
云裳 2025-01-01 03:05:38

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

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

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

connect(sender, &Sender::signalMethod,  receiver, &Receiver::slotMethod);
只是在用心讲痛 2025-01-01 03:05:38

有这样的工具吗?

如果存在这样的工具就好了,但不幸的是,由于 qt 中信号/槽机制的实现方式,它不存在。此外,由于这个原因,不可能静态地检查信号是否适合插槽。

如果 qt 使用类似 boost 的信号/槽之类的东西,那是可能的。

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.

凝望流年 2025-01-01 03:05:38

您可能会考虑用 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.

2025-01-01 03:05:38

我在我的代码中使用了类似的东西:

  #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__);

我使用它而不是对 connect() 的简单调用。对你有帮助吗??

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

云醉月微眠 2025-01-01 03:05:38

您无法在编译时检查这一点,但如果您在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文