我应该在传统回调中使用Qt信号/时隙机制吗? [英] Should I use Qt signal/slot mechanisms over traditional callbacks?

查看:97
本文介绍了我应该在传统回调中使用Qt信号/时隙机制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们团队中的一位高级开发人员在我们的Qt应用程序中使用了传统的 C风格的回调而不是使用Qt信号/时隙机制。

A senior developer in my team used traditional C-style callbacks in our Qt application instead of using Qt signal/slot mechanisms.

我的第一个反应是替换他的代码,改用Qt信号/插槽。

My first reflex would be to replace his code and use Qt signal/slot instead.

有没有在Qt应用程序/库中使用回调的充分理由?

Is there any good reasons to use callbacks in a Qt application/library?

谢谢。

推荐答案

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

I think the better approach would be to embrace the framework you are using and use signal/slots.

也就是说,如果所讨论的代码有效,并且不是丑陋或引起问题的,那么您最好不要管它。

That being said, if the code in question works, and is not ugly or causing problems, then you may be best to leave it alone.

咨询信号/插槽文档描述了为什么使用信号/插槽方法更好的原因:

Consulting the Signal/Slot documentation describes why the Signal/Slot approach is better:


回调有两个基本缺陷:
首先,它们不是类型安全的。我们
永远不能确定
处理函数会使用正确的参数调用
回调。
其次,回调是与处理函数
强烈耦合的
,因为处理函数必须
知道要调用哪个回调。

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

在大多数情况下,速度可能并不重要,但在某些情况下,反复打电话会有所作为。

The speed probably doesn't matter for most cases, but there may be some extreme cases of repeated calling that makes a difference.

这篇关于我应该在传统回调中使用Qt信号/时隙机制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆