Understanding of Qt messages

analyze

What is a signal, signal and slot (Signals&Slot) is the basis of QT programming, and it is also a great innovation of Qt. Because of the programming mechanism of signals and slots, it becomes more intuitive and simple to deal with the interactive operation of various components of the interface in Qt.
A signal is an event that is emitted in a particular situation.
A slot is a function that responds to a signal. A slot is a function, which is the same as a general C++ function. It can be defined in any part of the class, can have any parameters, and can also be called directly.
Write an example below to describe it. It is useless to use the official website. The official website is somewhat difficult to understand:
header file:

#ifndef SIGNALEXAMPLE_H
#define SIGNALEXAMPLE_H
#include <QObject>

class SignalExample : public QObject
{
    
    
Q_OBJECT
public:
    SignalExample(QObject *parent = nullptr);
    void send();
    void send2();
signals:
    void sendSignal1(int newValue);
    int sendSignal2(int newValue);

public slots:
    void receiveSignal1(int value);
    int receiveSignal2(int value);
};

#endif // SIGNALEXAMPLE_H

CPP file:

#include "signalexample.h"

SignalExample::SignalExample(QObject *parent) : QObject(parent)
{
    
    
    QObject::connect(this, &SignalExample::sendSignal1,
                     this, &SignalExample::receiveSignal1);
    QObject::connect(this, &SignalExample::sendSignal2,
                     this, &SignalExample::receiveSignal2);
}

void SignalExample::send()
{
    
    
    emit sendSignal1(10);
}

void SignalExample::send2()
{
    
    
    qDebug("enter function send2");
    int a = sendSignal2(10);
    qDebug("exit function send2 a=%d", a);

}

void SignalExample::receiveSignal1(int value)
{
    
    
    qDebug("enter function SignalExample::receiveSignal1 value=%d", value);
    qDebug("exit SignalExample::receiveSignal1");
}

int SignalExample::receiveSignal2(int value)
{
    
    
    return value;
}

main function:

#include <QCoreApplication>
#include <signalexample.h>

int main(int argc, char *argv[])
{
    
    
    QCoreApplication a(argc, argv);
    SignalExample signalExampleA;
    signalExampleA.send();
    signalExampleA.send2();



    return a.exec();
}

Implementation:

image.png

Summarize

The official website says:

An object emits a signal when its internal state changes in some way that might be of interest to the object's client or owner. Signals are public access functions and can be emitted from anywhere, but we recommend only emitting signals from the class that defines Signal and its subclasses.
When a signal is emitted, the slot connected to it normally executes immediately, just like a normal function call. When this happens, the signals and slots mechanism is completely independent of any GUI event loop. Once all slots have returned, the code following the emit statement executes. The situation is slightly different when using queued connections; in this case, the code following the emit keyword will continue execution immediately, while the slot will execute later.
If multiple slots are connected to a signal, when the signal is emitted, the slots will execute sequentially in the order in which they were connected.
Signals are automatically generated by the moc and cannot be implemented in .cpp files. They can never have a return type (i.e. use void).
Note on parameters: Our experience shows that signals and slots are more reusable if they don't use special types. If QScrollBar::valueChanged() uses a special type, such as the hypothetical QScrollBar::Range, then it can only be connected to slots designed specifically for QScrollBar. It is not possible to connect different input components together.

From the above, we can see that some places are different from what the official website says. The official website says that the signal cannot have a return value, but the return value can actually be used.

The official website says this:

image.png

Obviously not accurate, the official website theoretically said that signals and slots

So how should a programmer understand this thing? A simple understanding is the association of two functions. The signal is a special function, and the slot is an ordinary function, which is associated through connect.

If between different threads, the signal is placed in the queue

In the same thread, the slot function is called directly through the signal.

It's that simple.

Finally, a summary with a picture:

insert image description here

Guess you like

Origin blog.csdn.net/maokexu123/article/details/130725001