什么是信号和槽位?[英] What are signals and slots?

本文是小编为大家收集整理的关于什么是信号和槽位?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

有人可以简单地解释"信号和插槽"模式?

推荐答案

信号和插槽是解耦发件人(信号)和零接收器(插槽)的一种方式.假设您是一个系统,该系统具有您要对这些事件感兴趣的系统的任何其他部分进行的事件.您将使用信号和插槽模式.

发件人向事件发出信号(通常是通过调用与该事件/信号关联的函数)自动调用所有接收器的所有接收器.这使您可以根据需要在程序的一生中连接和断开接收器.

由于此问题被标记为C ++,这是 boost.signals 库有一个更彻底的解释.

其他推荐答案

我认为,当您将它们视为观察者模式或发布/订户模式.出版商端上有一个signal,例如buttonPressed(IdType).每当按下按钮时,都调用与该信号连接的所有插槽.插槽位于订户侧.例如,插槽可以为sendMail(IdType).

与事件"按下",插槽将知道按下哪个按钮,因为ID将被移交给. IdType表示发布者与订户之间的连接发送的数据类型.订户可能的操作将为connect(signal, slot),它可以与sendMail(IdType)连接,以便如果按下按钮,则称为特定插槽.

这样做的好处是,订户(插槽侧)不需要关心信号的详细信息.它只需要连接即可.因此,在这里我们有很多松散的耦合.您可以更改按钮实现,但是插槽的接口仍然相同.

qt Signals/slots/slots/slots boost信号以获取更多信息.

其他推荐答案

想象一下您的应用程序中有GUI.在大多数情况下,控制流不太线性,即,您没有明确的动作序列,而是要与GUI交互的用户(例如按钮,菜单等).

这本质上是事件驱动的模型,可以通过信号和插槽模式很好地实现.信号是由对象生成的事件(想想GUI组件),插槽是这些事件的接收器.

这是一个示例:想象您有一个复选框,以您的编程语言为对象表示.该复选框可能会发生多个事情:它可以切换,这又意味着它是设置或不设置的.这些是它可以发出的信号.我们将将其命名为Checkbexoggled,CheckboxSet和CheckBoxUnset.如您所见,在此示例中,Checkbox将始终在切换时发射Chebbextoggled信号,但也完全取决于状态的更改.

.

现在想象一下还有其他一些对象,即标签,为了这个示例,它总是作为对象存在,但可以"出现"和"消失",并且系统哔哔声(也由对象表示),可以简单地嘟.这些是这些对象的插槽.我们将它们称为" MessageAppear"," Messagedisappear"和" Beep".

假设您希望每次切换复选框时系统哔哔声,并且标签会根据用户检查还是清除复选框出现或消失.

.

因此,您将以下信号连接到以下插槽(左侧的信号,右侧插槽):

checkboxToggled -> beep
checkboxSet -> messageAppear
checkboxUnset -> messageDisappear

基本上就是这样.

信号和插槽也可能有参数.例如,使用设置数值值的滑块,您希望在用户移动滑块后立即发送更改的值以及发射信号:sliderChanged(int).

当然,要实际做一些有用的事情,您会编写一些自己的类,其中包含一些自己的信号和插槽.这是非常容易完成的,并且使用这些信号和插槽,您可以以事件驱动的方式与GUI或代码的其他部分进行交互的好方法.

请记住,信号和插槽通常是对称的,因为通常会有一个与插槽相对应的信号.例如,复选框可能会在切换时发出信号,但它也可能包含一个切换复选框本身的插槽.分离始终相互对立的复选框很容易实现.

本文地址:https://www.itbaoku.cn/post/627786.html

问题描述

Can someone explain in simple terms the "signals and slots" pattern?

推荐答案

Signals and slots are a way of decoupling a sender (the signal) and zero or more receivers (the slots). Let's say you a system which has events that you want to make available to any other part of the system interested in those events. Rather than hard-wiring the code that generates event to the code that wants to know about those events, you would use a signals and slots pattern.

When the sender signals an event (usually by calling the function associated with that event/signal) all the receivers for that event are automatically called. This allows you to connect and disconnect receivers as necessary during the lifetime of the program.

Since this question was tagged C++, here is a link to the Boost.Signals library which has a much more thorough explanation.

其他推荐答案

I think one can describe signals and slots best when you are looking at them as a possible implementation vehicle for the Observer Pattern or Publish/Subscriber Pattern. There is one signal, for example buttonPressed(IdType) on the Publisher Side. Whenever the button is pressed, all slots that are connected to that signal are called. Slots are on the Subscriber Side. A slot could for example be sendMail(IdType) .

Along with the event "button pressed", the slot would know which button was pressed, since the id would have been handed over. IdType represents the type of the data sent over the connection between the Publisher and the Subscriber. An operation possible for the Subscriber would be connect(signal, slot) which could connect buttonPressed(IdType) with sendMail(IdType), so that if the button is pressed, that particular slot is called.

The good thing about this is that the subscriber (the slot side) doesn't need to care about details of the signal. It just needs to connect. Thus, here we have a great deal of loose coupling. You can change the buttons implementation, but the interface for the slots would still be the same.

Look at Qt Signals/Slots or Boost Signals for more informations.

其他推荐答案

Imagine having a GUI in your application. Most of the time, control flow wouldn't be very linear, i.e. instead of having a clear sequence of actions you'd have a user that interacts with the GUI (like buttons, menus etc.).

This is essentially an event driven model which can be implemented quite nicely with the signals and slots pattern. signals are events which are generated by objects (think GUI components) and slots are the receivers of those events.

Here is an example: imagine you have a checkbox, represented as an object in your programming language. Multiple things can happen to that checkbox: it can be toggled, which in turn also means that it's either set or unset. Those are the signals which it can emit. We will name them checkboxToggled, checkboxSet and checkboxUnset. As you see, in this example, the checkbox will always emit the checkboxToggled signal when toggled, but also exactly one of the two other signals, depending on how the state changes.

Now imagine having some other objects, namely a label, which for the sake of this example always exists as an object but can "appear" and "disappear" and a system beep (also represented by an object), which can simply beep. Those are the slots those objects have. We will call them "messageAppear", "messageDisappear" and "beep".

Suppose you want the system beep to beep everytime the checkbox is toggled, and the label to appear or disappear depending on whether the user checked or cleared the checkbox.

You would thus connect the following signals to the following slots (signals on the left, slots on the right):

checkboxToggled -> beep
checkboxSet -> messageAppear
checkboxUnset -> messageDisappear

That's basically it.

signals and slots may also have arguments. For example, using a slider which sets a numerical value, you would like to send the changed value along with the emitted signal as soon as the user moved the slider: sliderChanged(int).

Of course, to actually do something useful you would write some own classes which would contain some own signals and slots. This is done quite easily and using these own signals and slots, you have a nice way to interact with the GUI or other parts of your code in an event driven manner.

Keep in mind that signals and slots are often symmetric in the sense that there may often be a signal corresponding to a slot. For example, a checkbox may emit a signal when toggled, but it may also contain a slot that toggles the checkbox itself. It would be easy to implement to separate checkboxes that are always set oppositely to each other.