简体   繁体   English

信号和插槽的用途是什么

[英]what's the utility of signals and slots

Actually I have been using signals and slots while programing in Qt. 实际上,我在Qt中编程时一直在使用信号和插槽。 The main use of signals and slots are in the UI interaction, for example a clicked button sends a signal that will invoke a slot to perform action. 信号和插槽的主要用途是在UI交互中,例如,单击按钮会发送信号,该信号将调用插槽以执行操作。 I know that it's possible to use signals and slots for non-gui application, for example a server side application. 我知道可以将信号和插槽用于非GUI应用程序,例如服务器端应用程序。 I looked for other libraries that offers signal and slots and I found boost library. 我寻找了其他提供信号和插槽的库,然后找到了升压库。 It's some how different from what I learned from Qt. 这与我从Qt中学到的有所不同。 And then I was wondering what's the real utility of signals and slots, since I can manually call a function to perform an action at a given time. 然后,我想知道信号和插槽的真正用途是什么,因为我可以在给定的时间手动调用函数来执行操作。

for example in boost here is an example of signals/slots : 例如在boost中,这是信号/插槽的示例:

#include <boost/signals2.hpp>
#include <iostream>

void mySlot(){
  std::cout << "I'm a slot" << std::endl;
}

void main(){
  boost::signals2::signal<void ()> sig;
  // Connecting the slot to the signal.
  sig.connect(mySlot);

  // Emitting a signal that will call mySlot 
  sig();

  return 0;
}

I could simply do the same thing with a simple call of mySlot() isn't it ? 我可以简单地通过mySlot()调用来做同样的事情,不是吗?

Not that what made me think is that when we try to connect two different slots, they are called in the same order than connection. 并不是让我想到的是,当我们尝试连接两个不同的插槽时,它们的调用顺序与连接相同。 and if the first slot is blocking ( try to add an infinite loop) the second slots will never be called ! 如果第一个插槽被阻塞(尝试添加无限循环),则第二个插槽将永远不会被调用! I guess that it's a kind of vector that stores the addresses of the functions and then iterate the loop and call one by one ! 我猜这是一种向量,它存储函数的地址,然后迭代循环并一个接一个地调用!

What's the magic behind the signals and slots ? 信号和插槽背后的魔力是什么? or it's only a kind of abstraction for the developer ? 还是对开发人员来说只是一种抽象?

Thank you in advance. 先感谢您。

In short : The main idea is decoupling in my opinion. 简而言之 :我认为主要思想是脱钩

In longer : With direct function calls, you could not have a well-separate establishment between the common functionality and the clients of it, for instance. 在更长的篇幅中 :例如,通过直接函数调用,您无法在通用功能与其客户端之间建立良好的分隔。 There would be a tight coupling unlike with signals and slots because you would need to know at the point of some condition meeting your criteria which methods exactly to call. 与信号和时隙不同,这将导致紧密耦合,因为您需要在满足您的条件的某些情况下知道确切地调用哪些方法。

If you would like to gain more freedom, like with many other OOP design patterns, you need something like signals and slots. 如果您想像其他许多OOP设计模式一样获得更多自由,则需要信号和插槽之类的东西。 When a common component, let us call it a library, emits a signal, it does not have to be aware of the (potentially not-yet-existing) client interfaces. 当我们将其称为库的公共组件发出信号时,它不必知道(可能尚未存在)客户端接口。 This makes the common component flexible enough. 这使得通用组件足够灵活。

It will also keep responsiveness of the application better since it is not a direct call, but processed by the Qt event loop. 由于它不是直接调用,而是由Qt事件循环处理的,因此它还将更好地保持应用程序的响应性。 Arguably, you could circumvent this with custom threading mechanisms, but that would be lotta more work to make it safe enough, and you would end up doing something close this in the end of the day. 可以说,您可以使用自定义线程机制来规避此问题,但是要使它足够安全,还需要做更多的工作,最后您将不得不做一些事。

Rednaks, Rednaks,

Signals and slots are simply a communication mechanism that can be used across different threads of execution. 信号和插槽只是一种通信机制,可以在不同的执行线程之间使用。 That is the main value (especially in a GUI focussed library such as QT). 那是主要的价值(尤其是在以GUI为重点的库(例如QT)中)。 Usually there is 1 thread responsible for drawing and managing the graphical side of the application and 1 or more worker threads. 通常,有1个线程负责绘制和管理应用程序的图形端,还有1个或更多工作线程。 Using signals and slots it is possible to abstract away from this. 使用信号和时隙可以从中抽象出来。 If you wanted to use direct function calls you'd need to synchronise the threads which would lead to reduced performance and responsiveness on the GUI side. 如果要使用直接函数调用,则需要同步线程,这将导致GUI端的性能和响应速度降低。

Indeed within one execution thread it does not make much sense using signals and slots. 实际上,在一个执行线程中,使用信号和插槽并没有多大意义。 The value comes in when you want to communicatie between processes (on the same machine or between different machines). 当您要在进程之间(在同一台计算机上或在不同计算机上)进行通信时,将使用该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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