Signals and slots in Qt (Signals and Slots)

Signals and slots in Qt are a mechanism for communication between objects, often used to handle user interface events and data updates. Through signals and slots, decoupling and flexible interaction between objects can be achieved.

Signal (Signal) is an event or notification sent by an object, which can be the name of any function, and is often declared with the emit keyword as a prefix. For example, a button click signal could be clicked().

Slot (Slot) is a function that receives a signal, is bound to the signal, and is used to process the event sent by the signal. Slot functions can be ordinary member functions, static functions, or even Lambda expressions. The declaration and definition of slot functions are no different from ordinary functions.

The steps to use signals and slots are as follows:

  1. Declare the signal in the object that sends the signal, for example, declare the signal in the QPushButton class clicked().

  2. A slot function is defined in the object receiving the signal to handle the events emitted by the signal.

  3. Use connect()functions to connect signals and slots, and perform this connection operation at the appropriate time. For example: connect(sender, SIGNAL(clicked()), receiver, SLOT(handleClick())). Newer versions of Qt can use a more modern syntax connect(sender, &Sender::signal, receiver, &Receiver::slot).

  4. A signal is emitted via the emit keyword when the object that sent the signal fires the associated event.

  5. After the signal is sent, the slot function connected to it will be automatically called to perform related operations.

Through signals and slots, flexible inter-object communication can be achieved without explicitly knowing or modifying each other's implementation details. This loosely coupled design makes the code more modular and maintainable.

It should be noted that the connection operation of signals and slots usually needs to be performed in QObject derived classes, and needs to be used in the event loop of the application.

The following is a simple Qt Widgets Application routine:
file name: mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    
    
    /* 设置窗体的宽为 1280,高为 720 */
    this->resize(1280, 720);

    /* 实例化 pushButton 对象 */
    pushButton = new QPushButton(this);

    /* 调用 setText()方法设定按钮的文本 */
    pushButton->setText("我是一个按钮");

    /* 信号与槽连接 */
    connect(pushButton, SIGNAL(clicked()), this,
            SLOT(pushButtonClicked()));

    connect(this, SIGNAL(pushButtonTextChanged()), this,
            SLOT(changeButtonText()));
}

MainWindow::~MainWindow()
{
    
    
}

/* 实现按钮点击槽函数 */
void MainWindow::pushButtonClicked()
{
    
    
    /* 使用 emit 发送信号 */
    emit pushButtonTextChanged();
}

/* 实现按钮文本改变的槽函数 */
void MainWindow::changeButtonText()
{
    
    
    /* 在槽函数里改变按钮的文本 */
    pushButton->setText("被点击了! ");
}

File name: mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
/* 引入 QPushButton */
#include <QPushButton>

class MainWindow : public QMainWindow
{
    
    
    Q_OBJECT

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

signals:
     /* 声明一个信号,只需声明,无需定义 */
     void pushButtonTextChanged();

public slots:
 /* 声明一个槽函数 */
 void changeButtonText();

 /* 声明按钮点击的槽函数 */
 void pushButtonClicked();

 private:
 /* 声明一个对象 pushButton */
 QPushButton *pushButton;

};
#endif // MAINWINDOW_H

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_37787043/article/details/131641243