Application Development in Linux: QT signal and Slot Mechanism
Source: Internet
Author: User
Article Title: Application Development in Linux: QT signal and slot mechanism. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
As the core mechanism of QT, signals and slots are widely used in QT programming, this article introduces some basic concepts of signals and slots, metadata tools, and some issues that should be paid attention to during actual use.
QT is a cross-platform C ++ GUI application framework. It provides a rich set of window components and features object-oriented, easy to expand, and true component programming, even more striking is that the most popular KDE Desktop Environment on Linux is built on the basis of the QT library. QT supports the following platforms: MS/WINDOWS-95, 98, NT and 2000; UNIX/X11-Linux, Sun Solaris, HP-UX, Digital Unix, ibm aix, sgi irix; EMBEDDED-Linux platform supporting framebuffer. With the rapid development and popularization of KDE, QT is likely to become the preferred GUI for software development on the Linux window platform.
I. Overview
The signal and slot mechanism is the core mechanism of QT. to be proficient in QT programming, you must understand the signal and slot. The signal and slot are an advanced interface used for communication between objects. It is the core feature of QT and an important part of distinguishing QT from other toolkit. The signal and slot is a communication mechanism defined by QT. It is independent from the Standard C/C ++ language. Therefore, it is necessary to correctly process the signal and slot, you must use a QT tool called moc (Meta Object Compiler), which is a C ++ Preprocessing Program that automatically generates additional code for high-level event processing.
In many well-known GUI toolkits, Widgets have a callback function to respond to each action they can trigger, this callback function is usually a pointer to a function. However, in QT, CITIC signs and slots replace these messy function pointers, making these communication programs simpler and clearer. Signals and slots can carry any number and any type of parameters. They are completely type-safe and do not generate core dumps like callback functions.
All classes derived from QObject or its subclass (such as Qwidget) can contain signals and slots. When an object changes its state, the signal is sent out by the object (emit). This is all the things the object has to do. It does not know who is receiving the signal at the other end. This is the true information encapsulation, which ensures that the object is used as a real software component. Slots are used to receive signals, but they are common object member functions. A slot does not know whether any signal is connected to itself. Moreover, the object does not understand the specific communication mechanism.
You can connect many signals to a single slot, or connect a single signal to many slots, or even connect one signal to another, at this time, no matter when the first signal is sent, the system will immediately launch the second signal. In short, signals and slots construct a powerful component programming mechanism.
2. Signal
When a signal changes the internal status of its customer or owner, the signal is transmitted by an object. Only the class that defines this signal and its derived class can transmit this signal. When a signal is sent, the associated slot is immediately executed, just like a normal function call. The signal-slot mechanism is completely independent of any GUI event loop. The emission function (emit) is returned only after all slots are returned. If multiple slots are associated with a specific signal, these slots will be executed one by one when the signal is sent, however, their execution order will be random and uncertain. We cannot artificially specify which is executed first and which is then executed.
The signal declaration is carried out in the header file. The signals keyword of QT indicates that it enters the signal declaration area and then declares its own signal. For example, three signals are defined below:
Signals:
Void mySignal ();
Void mySignal (int x );
Void mySignalParam (int x, int y );
In the above definition, signals is the key word of QT, rather than C/C ++. The next line void mySignal () defines the signal mySignal, which carries no parameters; the next line void mySignal (int x) defines the duplicate signal mySignal, but it carries an integer parameter, this is a bit similar to the virtual function in C ++. Formally speaking, the signal declaration is the same as the ordinary C ++ function, but the signal is not defined by the function body. In addition, the return type of the signal is void, do not expect any useful information from the signal.
Signals are automatically generated by moc, which should not be implemented in the. cpp file.
3. Slot
A slot is a common C ++ member function that can be called normally. Its unique characteristic is that many signals can be associated with it. This slot is called when the signal associated with it is sent. Slot can have parameters, but slot parameters cannot have default values.
Since the slot is a common member function, like other functions, they also have access permissions. The access permission of the slot determines who can be associated with it. Like common C ++ member functions, slots are also divided into three types: public slots, private slots, and protected slots.
Public slots: The slots declared in this region means that any object can connect the signal to it. This is very useful for component programming. You can create objects that do not know each other and connect their signals to the slot so that information can be correctly transmitted.
Protected slots: The slots declared in this region means that the current class and its subclass can connect signals to it. This applies to those slots, which are part of the class implementation, but their interface interfaces are external-oriented.
Private slots: The slots declared in this region means that only the class itself can connect the signal with it. This applies to closely related classes.
The slot can also be declared as a virtual function, which is also very useful.
The slot statement is also carried out in the header file. For example, the following three slots are declared:
Public slots:
Void mySlot ();
Void mySlot (int x );
Void mySignalParam (int x, int y );
Iv. Correlation Between signals and slots
Call the connect function of the QObject object to associate the signal of an object with the slot function of another object. In this way, when the transmitter emits a signal, the receiver's slot function is called. The function is defined as follows:
This function is used to associate the signal in the transmitter sender object with the member slot function in the receiver. The macro signal () of QT must be used when the SIGNAL signal is specified, and the macro SLOT () must be used when the SLOT function is specified (). If the initiator and receiver belong to the same object, the receiver parameter can be omitted in the connect call.
For example, the following two objects are defined: the label object and the scroll object of the scroll bar, And the valueChanged () signal is associated with the setNum () of the label object, in addition, the signal carries an integer parameter, so that the label always displays the value of the position of the scroll bar.
QLabel * label = new QLabel;
QScrollBar * scroll = new QScrollBar;
QObject: connect (scroll, SIGNAL (valueChanged (int )),
Label, SLOT (setNum (int )));
A signal can even be associated with another signal. See the following example:
Class MyWidget: public QWidget
{
Public:
MyWidget ();
...
Signals:
Void aSignal ();
...
Private:
...
QPushButton * aButton;
};
MyWidget: MyWidget ()
{
AButton = new QPushButton (this );
Connect (aButton, SIGNAL (clicked (), SIGNAL (aSignal ()));
}
In the preceding constructor, a private button aButton is created for MyWidget. The clicked () signal generated by the Click Event of the button is associated with another signal aSignal. In this way, when the signal clicked () is sent, the signal aSignal () is also sent. Of course, you can also associate a click event with a private slot function and then transmit aSignal () signals in the slot. This seems redundant.
When there is no need to continue to associate the signal with the slot, we can use the disconnect function to disconnect the connection. It is defined as follows:
This function disconnects the signal in the transmitter from the slot function in the receiver.
The disconnect () function must be used in three cases:
Disconnects any objects associated with an object. This seems a bit incomprehensible. In fact, when we define one or more signals in an object, these signals are associated with the slots in several other objects, if we want to cut off these associations, we can use this method, which is very simple.
Disconnect (myObject, 0, 0, 0)
Or
MyObject-> disconnect ()
Disconnects any association with a specific signal.
Disconnect (myObject, SIGNAL (mySignal (), 0, 0)
Or
MyObject-> disconnect (SIGNAL (mySignal ()))
Disconnects two objects.
Disconnect (myObject, 0, myReceiver, 0)
Or
MyObject-> disconnect (myReceiver)
In the disconnect function, 0 can be used as a wildcard to represent any signal, any receiving object, or any slot function in the receiving object. However, the sender's sender cannot be 0, and the values of the other three parameters can be equal to 0.
V. object meta Tool
The meta object compiler moc (meta object compiler) analyzes class declarations in the C ++ file and generates C ++ code for initializing the meta object, the object contains the names of all signals, slots, and pointers to these functions.
Moc reads the C ++ source file. If a class with a Q_OBJECT macro sound is found, it generates another C ++ source file, this newly generated file contains the meta object code of this class. For example, suppose we have a head file mysignal. h. This file contains a signal or a slot statement. Before compilation, the moc tool will automatically generate a file named mysignal Based on the file. moc. h C ++ source file and submit it to the compiler; similarly, it corresponds to mysignal. the cpp file moc tool will automatically generate a file named mysignal. moc. submit the cpp file to the compiler.
The metadata code is required by the signal/slot mechanism. The C ++ source file generated using moc must be compiled and connected together with the class implementation, or #
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service