Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between event and signal in Qt

Tags:

events

signals

qt

It is hard for me to understand the difference between signals and events in Qt, could someone explain?

like image 954
Sphinx Avatar asked Feb 17 '12 06:02

Sphinx


People also ask

What is a signal in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

What is event loop in Qt?

An event loop in a thread makes it possible for the thread to use certain non-GUI Qt classes that require the presence of an event loop (such as QTimer, QTcpSocket, and QProcess). It also makes it possible to connect signals from any threads to slots of a specific thread.

How do Qt signals work?

The Qt signals/slots and property system are based on the ability to introspect the objects at runtime. Introspection means being able to list the methods and properties of an object and have all kinds of information about them such as the type of their arguments.

Are Qt signals asynchronous?

So in normal cases, it will be synchronous and blocking, and with queued connections it will be asynchronous and non-blocking.


3 Answers

An event is a message encapsulated in a class (QEvent) which is processed in an event loop and dispatched to a recipient that can either accept the message or pass it along to others to process. They are usually created in response to external system events like mouse clicks.

Signals and Slots are a convenient way for QObjects to communicate with one another and are more similar to callback functions. In most circumstances, when a "signal" is emitted, any slot function connected to it is called directly. The exception is when signals and slots cross thread boundaries. In this case, the signal will essentially be converted into an event.

like image 116
Arnold Spence Avatar answered Oct 11 '22 13:10

Arnold Spence


Events are something that happened to or within an object. In general, you would treat them within the object's own class code.

Signals are emitted by an object. The object is basically notifying other objects that something happened. Other objects might do something as a result or not, but this is not the emitter's job to deal with it.

like image 23
laurent Avatar answered Oct 11 '22 13:10

laurent


My impression of the difference is as follows:

enter image description here

Say you have a server device, running an infinite loop, listening to some external client Events and reacting to them by executing some code.

(It can be a CPU, listening to interrupts from devices, or Client-side Javascript browser code, litsening for user clicks or Server-side website code, listening for users requesting web-pages or data).

Or it can be your Qt application, running its main loop.

I'll be explaining with the assumption that you're running Qt on Linux with an X-server used for drawing.

I can distinguish 2 main differences, although the second one is somewhat disputable:

  1. Events represent your hardware and are a small finite set. Signals represent your Widgets-layer logic and can be arbitrarily complex and numerous.

Events are low-level messages, coming to you from the client. The set of Events is a strictly limited set (~20 different Event types), determined by hardware (e.g. mouse click/doubleclick/press/release, mouse move, keyboard key pressed/released/held etc.), and specified in the protocol of interaction (e.g. X protocol) between application and user.

E.g. at the time X protocol was created there were no multitouch gestures, there were only mouse and keyboard so X protocol won't understand your gestures and send them to application, it will just interpret them as mouse clicks. Thus, extensions to X protocol are introduced over time.

X events know nothing about widgets, widgets exist only in Qt. X events know only about X windows, which are very basic rectangles that your widgets consist of. Your Qt events are just a thin wrapper around X events/Windows events/Mac events, providing a compatibility layer between different Operating Systems native events for convenience of Widget-level logic layer authors.

Widget-level logic deals with Signals, cause they include the Widget-level meaning of your actions. Moreover, one Signal can be fired due to different events, e.g. either mouse click on "Save" menu button or a keyboard shortcut such as Ctrl-S.

  1. Abstractly speaking (this is not exactly about Qt!), Events are asynchronous in their nature, while Signals (or hooks in other terms) are synchronous.

Say, you have a function foo(), that can fire Signal OR emit Event. If it fires signal, Signal is executed in the same thread of code as the function, which caused it, right after the function.

On the other hand, if it emits Event, Event is sent to the main loop and it depends on the main loop, when it delivers that event to the receiving side and what happens next.

Thus 2 consecutive events may even get delivered in reversed order, while 2 consecutively fired signals remain consecutive.

Though, terminology is not strict. "Singals" in Unix as a means of Interprocess Communication should be better called Events, cause they are asynchronous: you call a signal in one process and never know, when the event loop is going to switch to the receiving process and execute the signal handler.

P.S. Please forgive me, if some of my examples are not absolutely correct in terms of letter. They are still good in terms of spirit.

like image 35
Boris Burkov Avatar answered Oct 11 '22 14:10

Boris Burkov