在Qt世界中,事件和信号/插槽有什么区别?
In the Qt world, what is the difference of events and signal/slots?
一个会取代另一个吗?事件是信号/槽的抽象吗?
Does one replace the other? Are events an abstraction of signal/slots?
Qt 文档 可能解释得最好:
在Qt中,事件是对象,派生的来自抽象的 QEvent
类,即代表已经发生的事情无论是在应用程序中还是作为外部活动的结果应用程序需要了解.事件可以被接收和处理QObject
子类的任何实例,但它们与小部件.本文档描述了如何事件在一个典型应用.
In Qt, events are objects, derived from the abstract
QEvent
class, that represent things that have happened either within an application or as a result of outside activity that the application needs to know about. Events can be received and handled by any instance of aQObject
subclass, but they are especially relevant to widgets. This document describes how events are delivered and handled in a typical application.
所以事件和信号/槽是完成相同事情的两个并行机制.通常,一个事件将由外部实体(例如,键盘或鼠标滚轮)生成,并通过 QApplication
中的事件循环传递.通常,除非您设置代码,否则您不会生成事件.您可以通过 QObject::installEventFilter()
过滤它们或通过覆盖适当的函数来处理子类对象中的事件.
So events and signal/slots are two parallel mechanisms accomplishing the same things. In general, an event will be generated by an outside entity (for example, keyboard or mouse wheel) and will be delivered through the event loop in QApplication
. In general, unless you set up the code, you will not be generating events. You might filter them through QObject::installEventFilter()
or handle events in subclassed object by overriding the appropriate functions.
信号和插槽更容易生成和接收,您可以连接任意两个 QObject
子类.它们是通过元类处理的(更多信息请查看您的 moc_classname.cpp 文件),但是您将生成的大多数类间通信可能会使用信号和槽.信号可以立即传递或通过队列延迟传递(如果您使用线程).
Signals and Slots are much easier to generate and receive and you can connect any two QObject
subclasses. They are handled through the Metaclass (have a look at your moc_classname.cpp file for more), but most of the interclass communication that you will produce will probably use signals and slots. Signals can get delivered immediately or deferred via a queue (if you are using threads).
可以生成信号.
这篇关于Qt 事件和信号/插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!