Lesson2-signal and slot mechanism. Lesson2-signal and slot mechanism I. understanding of signals and slots 1. what are signals and slots? In many GUI tools, each widget has a callback function to respond to the lesson2-signal and slot mechanisms they can trigger
I. understanding of signals and slots
1. what are signals and slots?
In many GUI tools, each widget has a callback function to respond to the actions they can trigger. these callback functions are usually a pointer to a function. Pointer is a relatively troublesome concept. in Qt, CITIC signs and slots replace these messy function pointers, making programming easier.
When an object changes, the signal is sent out from this object. this is all the things the object is going to do. it does not know who is receiving the signal at the other end, this process is truly information encapsulation, achieving absolute security.
Slots are used to receive signals. they are common c ++ member functions. they do not know whether a signal is associated with themselves, and the objects do not understand the communication mechanism.
In Qt, when you click a button, this button will send a signal, and this signal will be transmitted to another object, and the object receiving the signal will produce the corresponding action, this is the signal and slot mechanism.
2. correlation between signals and slots
1) signal and slot pass
QObject: connect (const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt: Connection type = Qt: AutoCompatConnection) Association
Sender, the sender of the signal
Signal, signal to be sent
Receiver, the receiver of the signal.
Method, slot function
Type, correlation between signals and slots
2) There are three types of types
Qt: DirectConnection, which is passed to the slot immediately after the signal is sent. after the slot function is executed and returned, the system sends the "emit" signal. <信号> "Subsequent code can be executed
Qt: QueueConnection. after the signal is sent, it is queued until the event loop has the ability to pass it to the slot. no matter whether the slot function is executed or not, it sends the "emit" signal. <信号> "Subsequent Code
Executed immediately
Qt: AutoCompatConnection. if the signal and the slot are in the same thread, it is equivalent to Qt: DirectConnection. if the signal and the slot are not in the same thread, it is equivalent
Qt: QueueConnection.
II. signal and slot instances
1. signal
When the internal state of an object changes, the signal is sent. only the class that defines a signal and its subclass can transmit the signal. For example, the QPushButton class predefines the clicked signal. when the button is clicked, the clicked signal is sent.
In addition to the predefined signals, you can also customize the signals in Qt.
The signal declaration starts with the keyword signals in the header file.
Signals:
Void signals ();
Void signals (int x );
There are two signals here, with the same name but different parameters, which is similar to function overloading.
The signal is similar to a common C ++ function, but it has no entity and only one shell, and its return value is always void. do not expect to return useful information from the signal. After the signal is declared, a moc tool is automatically generated, so it should not be placed in the. cpp file.
System pre-defined signals are generally automatically transmitted, which does not need to be concerned. if you need to manually transmit signals, you can write emit signals () in this way ();
2. slot
The slot is a common C ++ member function and can be called normally. many signals can be associated with the slot. Since the slot is a common member function, it also has permissions. Therefore, there are three types of slots, public slots, private slots, and protect slots.
The slot function statement is also in the header file.
Private slots:
Void mySlot1 ();
Void mySlot1 (int x );
Before declaring the signal and slot functions, you must add a macro Q_OBJECT. Only in this way can Qt recognize them.
3. object meta tool
The moc tool initializes the class declaration in the c ++ file and automatically generates the c ++ code used to initialize the object. the object contains all the signals and slots, and
Needle.
Moc reads the c ++ source file. if a class with a Q_OBJECT macro sound is found, it generates another c ++ source file. Moc tool is not extended # define, So macro definition is not available in signal and slot functions
4. Association
Connect function
Btn = new QPushButton ();
Lab = new QLabel ();
Connect (btn, SIGNAL (clicked (), lab, SLOT (clear ()))
Macro SIGNAL () is required for the SIGNAL, and macro SLOT () is required for the SLOT function ();
The Signal and slot parameters must be consistent;
When the signal and the slot belong to the same object, the receiver of the signal can be omitted
Connect (btn, SIGNAL (clicked (), SLOT (showMenu ()))
Notes for signals and slots
1) The constructor cannot be in the signal and slot area.
2) function pointers cannot be used as parameters for signals and slots.
3) the signal and slot cannot have default parameters.
4) the signal and slot cannot contain template parameters.
5) Nested classes cannot be located in the signal and slot areas, or have signals and slots.
Signal 1. understanding of signals and slots 1. what are signals and slots? In many GUI tools, each widget has a callback function to respond to the calls they can trigger...