QT study notes: QT signals and slots

First, the basic concept of signal and slot (Signal & Slot)

1. Understand signals and slots

(1) Signal (Signal): An event that is emitted in a specific situation. For example, the most common signal of PushButton is the clicked() signal emitted when the mouse is clicked; (2) Slot (Slot): a function that responds to the 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 (public, private or protected), can have any parameters, and can also be called directly.
    Signals and slots realize the communication between objects. When an object changes, it will send a signal to notify another object. When another object receives the signal, it will execute a function. This function is called a slot. Simply put, it is the "publish-subscriber" model.

    To realize the binding of signals and slot functions, we need to do the following steps.

  (1) Declare this slot function in the header file;

  (2) Define this slot function in the .cpp file, that is, write down the content of the activity that needs to be done;

  (3) Connect the signal and slot in the constructor in the .cpp file, using the connect() function.

2. Automatic binding of signals and slots

    (In addition to using the connect() function, signals and slots can also be automatically connected without connect(), but the slot function name needs to be set to, for example: on_function name_clicked)

    For example, on the designed ui interface, place the mouse on the pushButton, right-click, and choose "Go to slot...".

     At this time, the following window will appear. If the button click is valid, select click(), and then click OK. At this point, the following functions will be automatically generated:

void Dialog::on_MyButton_clicked()
{
    //添加自己的业务逻辑
}

3. Manual binding of signals and slots

(1) Declaration of signal

signals:
    void mysignal_1();
    void mysignal_2(int data);

QT comes with many controls, and these controls themselves define many signals, such as button clicked(), pressed(), etc.

(2) Slot declaration

public slots:
    void myslot_1();
    void myslot_2(int data);

(3) Binding of signals and slots

connect(sender, SIGNAL(signals), receiver, SLOT(slot));

(4) Transmit signal

emit mySignal_1(); // 发射信号mySignal_1

(5) Connection rules for signals and slots

(5.1) Signal parameters can be more than slot functions, and vice versa
(5.2) A signal can be connected to multiple slots
(5.3) Multiple signals can be connected to a slot
(5.4) A signal can be connected to another signal

Two, examples

Create a project with Qt Creator.

1、mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow

{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    int a = 0;

signals: //声明信号
    void mySignal_1();
    void mySignal_2(int data);

public slots: //声明槽函数
    void mySlot_1();
    void mySlot_2(int data);
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

2、mainwindow.cpp


#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->pushButton->setText("Hello World");

    //信号与槽的连接
    connect(this, SIGNAL(mySignal_1()), this, SLOT(mySlot_1()));
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(mySlot_1()));

    //发射信号
    emit mySignal_1();
}

MainWindow::~MainWindow()
{
    delete ui;
}

//槽函数的实现
void MainWindow::mySlot_1(){
    qDebug() << "call mySlot_1()";
    emit mySignal_2(10000);
}

//槽函数的实现(统计按下按钮次数,并在输出显示中输出)
void MainWindow::mySlot_2(int data){
    qDebug() << "call mySlot_2(), data:" << data;
    a++;
    qDebug() << "第" << a << "次按下按钮";
}

//这个pushButton_clicked方法,直接在ui界面右键转到槽就可以自动生成
void MainWindow::on_pushButton_clicked(){
    qDebug() << "on_pushButton_clicked()";
}

 3. Execution result:

(1) After the program starts, it will output: call mySlot_1()

 (2) When the button is clicked, the output is as follows:

 (3) Next, I associate signal_2 with slot_2 (there are only types in connect - such as int, no parameters)

connect(this, SIGNAL(mySignal_2(int)), this, SLOT(mySlot_2(int)));

The execution results are as follows:

 

reference:

(1) Qt Creator: Getting to know signals and slots first

(2) Qt6 Tutorial III (13) TCP/IP Communication and Socket Programming

Guess you like

Origin blog.csdn.net/mars21/article/details/131517157