c++ - 我应该在传统回调上使用 Qt 信号/槽机制吗?

标签 c++ qt callback

我团队中的一位高级开发人员使用传统的 C-style callbacks在我们的 Qt 应用程序中,而不是使用 Qt 信号/槽机制。

我的第一 react 是替换他的代码并改用 Qt 信号/槽。

在 Qt 应用程序/库中使用回调有什么好的理由吗?

谢谢。

最佳答案

我认为更好的方法是接受您正在使用的框架并使用信号/插槽。

话虽这么说,如果有问题的代码有效,并且不难看或导致问题,那么最好不要管它。

咨询Signal/Slot documentation描述了为什么 Signal/Slot 方法更好:

Callbacks have two fundamental flaws: Firstly, they are not type-safe. We can never be certain that the processing function will call the callback with the correct arguments. Secondly, the callback is strongly coupled to the processing function since the processing function must know which callback to call.

请注意以下事项:

Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide

在大多数情况下,速度可能无关紧要,但在某些极端情况下,重复调用可能会产生影响。

关于c++ - 我应该在传统回调上使用 Qt 信号/槽机制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3007250/

相关文章:

c++ - 访问静态变量时未解析的外部符号

c++ - 处理闪现窗口事件

c++ - Qt 如何处理信号和槽的引用调用?

c++ - 函数和数组

c++ - Luabind 编译时错误 Unable to deduce template argument

node.js - Promise 超时后停止执行

javascript - 异步加载 javascript - 如何进行回调?

javascript - 从回调函数中获取值

c++ - 错误 : Type 'EGL_DEFAULT_DISPLAY' could not be resolved when tried to use Open GL purely in C++ NativeActivity in NDK

c++ - 为什么在构造函数中使用 this 指针的 QState 子类内的嵌套 QStateMachine 会导致外部状态机无法进行转换?