問題描述
在Qt世界中,事件和信號/插槽有什么區(qū)別?
In the Qt world, what is the difference of events and signal/slots?
一個(gè)會取代另一個(gè)嗎?事件是信號/槽的抽象嗎?
Does one replace the other? Are events an abstraction of signal/slots?
推薦答案
Qt 文檔 可能解釋得最好:
在Qt中,事件是對象,派生的來自抽象的 QEvent
類,即代表已經(jīng)發(fā)生的事情無論是在應(yīng)用程序中還是作為外部活動的結(jié)果應(yīng)用程序需要了解.事件可以被接收和處理QObject
子類的任何實(shí)例,但它們與小部件.本文檔描述了如何事件在一個(gè)典型應(yīng)用.
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.
所以事件和信號/槽是完成相同事情的兩個(gè)并行機(jī)制.通常,一個(gè)事件將由外部實(shí)體(例如,鍵盤或鼠標(biāo)滾輪)生成,并通過 QApplication
中的事件循環(huán)傳遞.通常,除非您設(shè)置代碼,否則您不會生成事件.您可以通過 QObject::installEventFilter()
過濾它們或通過覆蓋適當(dāng)?shù)暮瘮?shù)來處理子類對象中的事件.
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.
信號和插槽更容易生成和接收,您可以連接任意兩個(gè) QObject
子類.它們是通過元類處理的(更多信息請查看您的 moc_classname.cpp 文件),但是您將生成的大多數(shù)類間通信可能會使用信號和槽.信號可以立即傳遞或通過隊(duì)列延遲傳遞(如果您使用線程).
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).
可以生成信號.
這篇關(guān)于Qt 事件和信號/插槽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!