Qt——Signal and slot knowledge summary

Table of contents

1. Meaning and usage

(1). Meaning

(2) Use of .connect

(3). Signal function

(4). Slot function

2. Example


1. Meaning and usage

(1). Meaning

Signals and slots are a common means of communication in qt. The sender sends a signal, and the receiver receives it through the slot and completes a specific task.

Simply put, by sending a function (signal), the receiver completes the task through another function (slot).

(2) Use of .connect

In terms of usage, connect through the connect function.

connect(sender address, signal function, receiver address, slot function);

For example, let's take the following example:

Close the window by clicking the button.

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::myWidget)
{
    ui->setupUi(this);
    but = new QPushButton("close", this);
    but->move(200, 200);
    //将按钮clicked信号函数与QWidget类的close槽函数连接
    connect(but, &QPushButton::clicked, this, &QWidget::close);
}

 When we click on the window, the clicked function is executed to trigger close to close the window.

If the signal function or slot function used has overload , then it is impossible to pass the function directly by address, because the parameters cannot be distinguished.

At this time, the function pointer needs to be used to complete the call of the specified overloaded version.

Examples are as follows:

A* pa = new A;
B* pb = new B;
void(A::*APtr)(QString) = &A::funcA;
void(B::*BPtr)(QString) = &B::funcB;
//指定调用参数为QString的重载版本
connect(pa, APtr, pb, BPtr);

There are a few points to note when using connect:

1. The parameter types of the signal function and the slot function must match, and the number of parameters of the signal function can be more than that of the slot function, but it must not be less .

2. A signal function can connect to multiple slot functions

3. Multiple signal functions can be connected to the same slot function

Versions below QT4 do not support this connection form, and can only be used in the following form:

connect( sender address, SIGNAL( signal function (parameter) ), receiver address, SLOT( slot function (parameter) ) );

SIGNAL and SLOT are macros. When compiling, the signal function and slot function will be replaced with strings, and it will not be judged whether the parameters match each other. Therefore, as long as it is not a Qt version problem, this method is not recommended.

Use disconnect to disconnect the connection between the signal and the slot, and the parameters are consistent with connect. 

(3). Signal function

If we customize the signal function, there are a few points to note:

1. The signal function needs to be declared with the signals keyword

class A : public QObject
{
    Q_OBJECT
public:
    explicit A(QObject *parent = nullptr);

signals://以下均为信号函数
    void funcA();
    void funcA(QString str);
};

2. The signal function cannot be defined, only declared.

3. The return value is void

4. Can take parameters, can be overloaded

5. You can use the emit keyword to actively release the signal to trigger the slot function.

void test{
    emit A()->funcA();//使用无参版本
}

(4). Slot function

There are also a few points to note about custom slot functions:
1. It can be declared as any member function of a class (generally public) , and earlier versions must be declared under the public slots keyword.

2. The return type is void

3. Can take parameters and overload

4. You can use lambda expressions to replace

2. Example

We write a program that opens a window when the button is pressed and the button displays "close", and presses the close window button again to become "open".

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::myWidget)
{
    ui->setupUi(this);
    //设置窗口
    setFixedSize(500, 500); 
    //设置按钮
    but = new QPushButton("open", this);
    but->move(200, 200);
    but->setCheckable(true);//如果使用toggled必须先使用该函数,clicked亦可。

    mw = new QWidget;//窗口
    mw->setWindowTitle("Gap");//窗口名
    //使用lambda表达式代替槽函数
    connect(but, &QPushButton::toggled, this, [&]()mutable{
        if(but->text() == "open"){
            mw->show();
            but->setText("close");

        }
        else{
            mw->close();
            but->setText("open");

        }
    });

    //定义一个按钮end,按下直接关闭myWidget窗口退出程序
    QPushButton* end = new QPushButton("end", this);
    but->move(100, 200);
    connect(end, &QPushButton::clicked, this, &QWidget::close);
}

There is only one way for programmers to die when they are constantly charging: native death—— Weiming


Please correct me if there is any mistake 

Guess you like

Origin blog.csdn.net/weixin_61857742/article/details/128351351