Signals and slots in Qt: installation, features of work, creation

A signal is a message sent from an object when it enters an event. For its part, the slot is a common function associated with the signal that must be executed when it is received. You can associate a signal with several slots so that as a result of one event several functions are performed. In addition, this concept allows you to associate several signals in one slot, in which case the same function will be performed with several events.

Qt signals and slots help enable an event-oriented feature in the graphical user interfaces of applications. Users will not have to spend time creating links to signal slots one by one. Since many infrastructure classes provide them with different access to predefined Qt signals and slots.

Signals and Slots: Distinctive Features

Signals and slots made Qt a fun and innovative tool. This is a method in which “one object” ensures that when “something happened”, then the “other object” responds to what happened. This can be expressed in pseudo-code.

Signals and Slots: Distinctive Features




Four phrases in quotation marks in the example are arguments to the function call in pseudo-code. One of the typical ways to write a connect statement is through argument alignment. This has an advantage over the typical mechanism used in standard C or C ++, such as callbacks with function pointers enclosed in std :: function.





If the sending object is destroyed, it cannot emit any signal, since it is a member function of its class. But in order for the sender to call the recipient, he needs to point to it. The user will not need to worry that the recipient will be destroyed and become invalid, this is done automatically by the library. This operation scheme makes Qt signals and slots safe by default.

New communication mechanism

Qt provides some language additions through macros, which are processed by the Meta-object Compiler (MOC) code generator tool. This leads to more advanced introspection support with special mechanisms in classes in Qt. These include the approach of signals and slots of the communication object.

A connection is established by calling QObject :: connect (), which allows only those that have compatible arguments to be connected, thus ensuring security. Objects are not aware of the signals to which their slots subscribe, thereby maintaining a weak connection of objects. An example of how the “normal” C ++ class can be extended to support signals and slots and how this behavior is implemented can be seen below.

New facility communication mechanism




These special macros are selected by the MOC code generator tool, which generates the corresponding classes and functions that implement all the mechanisms specific to Qt. They must be performed by the application user. The following is a possible form of the slot Counter :: setValue ().





Possible slot form




You can see the emit statement to wait for Qt to signal valueChanged () with data as an argument when a new value is assigned.

Updated functionality

Starting with Qt 5.0, there is a new overload of QObject :: connect, which supports the transfer of data through Qt signals as the second and fourth arguments to a function pointer indicating the one to be called.

The new syntax allows you to call not only a member function declared as a slot in the header with public slots, but also any function. There is another use case when you need to declare functions as slots to make them usable for any other that is implemented at run time. It could be, for example, QML.

You can connect to any “called” function, which can be a standalone, lambda function or a member of an object that is not a derivative of QObject.

Updated functionality




When connected to a lambda, there is a recipient object, the lambda itself, but there is no signature that can be specified, since this is a function call statement. And when there is a free-standing function and signature, the third and fourth arguments of the first two calls are combined.

Symmetry and Security

Symmetry and Security




Starting with Qt 5.2, you can get the best of both implementations and a well-balanced join operator with two objects and two functions. In this version of Qt, an additional overload was added and now it is possible to pass, as the third argument, a "context object", which serves to automatically disconnect and ensures no problems.

Qt has utility classes that QObject automatically processes, for example, QScopedPointer QObjectCleanupHandler. If the user has some part of the application that uses the Qt classes, but does not have a closely related user interface, he will be able to find a way to use them as members of a class that is not based on QObject.

Comprehensive library

Comprehensive library




Qt has a set of libraries called QtCore, covering a set of basic functions:

  1. An application environment with event loops and an event system.
  2. A graphical interface system that is one of the strongest assets.
  3. Qt template containers such as lists, queues, vector and maps. They provide a great alternative to STL containers, providing mechanisms for iterating in Java or STL style and converting to STL counterparts.
  4. Resource Management Classes.
  5. A model representation system for separating data and their representations based on a set of abstract classes.
  6. Framework for Qt streams of signal slots.
  7. String processing and regular expressions.
  8. OS features such as file processing, printing, networking, and system settings.

In addition, there are several other packages, most of which are mature and multifunctional in the areas of: XML processing, multimedia capabilities, SQL database support, unit testing, OpenGL, WebKit integration, libraries, internationalization tools, SVG manipulation, D-bus communication Bus, ActiveX controls and others.

Resource management

The two resource management mechanisms in Qt are the hierarchy of ownership and implicit sharing. The ownership hierarchy consists of a tree of objects that handles the destruction of descendants. Whenever a new QObject-based object is created on the heap using new, the parent QObject is assigned to it, which ultimately leads to the creation of a hierarchical tree of objects. When an object is destroyed in a tree, all its descendants are also destroyed.

Implicit sharing refers to the internal mechanism, mainly used for Qt containers and large objects. It implements the copy-on-write approach, that is, it transfers a pointer to the data structure only for each assignment and copies the full data structure only in the case of a write operation. This ensures that unnecessary copies are not created from large data structures, which improves performance.

Create your own signals and slots

Users do not need to rely solely on the signals provided by widgets; you can create a Qt signal using the Signal class:

• from PySide.QtCore import Signal;

• tapped = Signal ().

Then, when the conditions for the object to which they are connected are satisfied, they call the signal method, and it is emitted, calling any Qt slots to which it is connected: thing.tapped.emit ().

This is good for two reasons, firstly, it allows users of objects to interact with objects in familiar ways. Secondly, it allows more flexible use of them, leaving the effects of determining actions on them for using code.

A simple example of generation

You can define a simple PunchingBag class that does only one thing, when it is called “punch”, it generates a punched signal.

A simple example of generation




The Punching Bag inherits the trait from QObject so that it can emit signals, it has a punched signal that carries no data and a punch method that only emits a punched Qt signal. To make the Punching Bag useful, you need to connect its punched signal to the slot. They identify a simple function that prints to the console, create an instance of the Punching Bag, and connect its punched signal to the slot.

If you put all this into a script and run it, it will provide an implementation of the reaction to perforation using the code.

One of the most interesting things you can do when using Qt creator is to create signals to make them carry data. For example, you can make the signal carry an integer, thus:

  • 1 updated = Signal (int);
  • 1 updated = Signal (str).

The data type can be any Python type name or a string identifying the C ++ data type.

PySide / PyQt Dispatch Circle

Circle is inherited from QObject so that it can emit signals. Signals are created with the signature of the slot to which they will be connected. The same signal can be emitted in several places.

PySide / PyQt Dispatch Circle




Now define some slots that can be connected to the signals of the Circle. To do this, there are signals that carry data. In order for a slot to receive data from a signal, it is determined with the same signature as the signal.

The code is very simple and intuitive. Next, create a Circle instance by connecting the signals to the slots, move and resize it.

When the received script is run, the output should be:

  • Circle was moved to (6, 5).
  • Circle was resized to radius 5.

Platform benefits

Qt has evolved into a development platform for both desktop computers and mobile devices, ranging from personal use to industrial or embedded software.

Slot and data from the signal




There is a toolkit that is provided with Qt using the development process. It includes the following:

  1. Qt creator signals and slots, a simple IDE that supports various build configurations and has strong support for debugging code with Qt support,
  2. Qt Designer, a graphical GUI editor that can also be integrated into other IDEs,
  3. Qt Linguist, an internationalized text manager that is easily handled by Qt.
  4. Qt Assistant, a help application that includes documentation for the Qt API, but can also be integrated into native applications to provide a help system.

The system uses breakpoints or qDebug to verify that the signal and slot code are definitely reached:

  • connect operator;
  • code where the signal is given;
  • slot code.

Tips and tricks

You can connect the signal directly to the signal of another object. What is used, for example, to send signals.

Tips and tricks




Below are some guidelines for troubleshooting signals and slots in the Qt C ++ library:

  1. Check compiler warnings about non-existent signals or slots.
  2. Make sure that the types of signal and slot parameters are accurate and, if necessary, correspond to each other.
  3. Make sure that no name is added to the argument of the signal or slot, for example, textChanged (const QString &) is used, and not textChanged (const QString & text).
  4. Validate the arguments and connection syntax.
  5. They check the brackets, make sure that SIGNAL and SLOT are capitalized, and that the sender and receiver are objects, not class names.
  6. Make sure the signal is triggered as expected. You can do this with the following code: connect (this, SIGNAL (Signal ()), qApp, SLOT (aboutQt ())).
  7. Make sure that the slots are correctly declared in the corresponding sections of the public / protected / private slots class declarations.
  8. Be sure to use the private slots :. The colon is checked, that is, private slots: and not private slots.
  9. When using custom signals, make sure that they are declared correctly, with the return type “void”, in the section of open / protected / private signals of the class declaration.

Qt is a traditional structure that constantly adapts to the requirements of modern user graphic interfaces. Unsurprisingly, this selection of tools written in C ++ is one of the solutions for developing more independent graphical user interfaces. First of all, such tools as signals and slots implemented in the latest versions, which not only optimize the work process as a whole, but also facilitate collaboration between designers and programmers, are responsible for this reputation.




All Articles