• Qt的信号和槽机制


    在C++中,对象与对象之间通信,要通过调用成员函数的方式来完成。而在Qt中提供了一种对象间的通信方式:信号与槽的机制。

    Qt通过QObject来提供这个通信机制。它的工作方式也很简单,通过QObject对象提供的connect连接函数将信号与处理信号的函数进行绑定,当信号被发出时,绑定到这个信号的函数都会被调用。
    槽(slot)本质上就是C++类的成员函数,它可以是任何成员函数、静态成员函数、全局函数、Lambda表达式。QObject的连接函数:

    connect(sender,SIGNAL(signal),receiver,SLOT(slot));
    
    • 1
    1. sender:发出信号的对象
    2. signal:发出的信号
    3. receiver:接收信号的对象
    4. slot:处理信号的函数

    一个信号可以绑定到多个槽函数:

    connect(sender,SIGNAL(signal),receiver,SLOT(slot1));
    connect(sender,SIGNAL(signal),receiver,SLOT(slot2));
    
    • 1
    • 2

    多个信号可以绑定到同一个槽函数:

    connect(sender,SIGNAL(signal1),receiver,SLOT(slot));
    connect(sender,SIGNAL(signal2),receiver,SLOT(slot));
    
    • 1
    • 2

    一个信号还可以连接到另一个信号:

    connect(sender,SIGNAL(signal1),receiver,SIGNAL(signal2));
    
    • 1

    解除信号与槽函数之间的绑定:

    disconnect(sender,SIGNAL(signal),receiver,SLOT(slot));
    
    • 1

    注意:信号与槽机制同真正的回调函数相比,时间的消耗还是非常大的,在实时系统(嵌入式实时系统)中要少用,甚至不用。

    举个使用信号与槽机制通信的Qt例子:
    sender.h

    #ifndef SENDER_H
    #define SENDER_H
    
    #include 
    
    class Sender : public QWidget
    {
        Q_OBJECT // 只有继承了QObject类的类才具备使用信号和槽机制通信的能力。
    public:
        explicit Sender(){};
        void send(const QString &name);
    
    signals:
        void message(const QString &name);//自定义的信号,它只有是一个函数声明,不需要实现,要求返回类型必须是void。
    
    };
    
    #endif // SENDER_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    receiver.h

    #ifndef RECEIVER_H
    #define RECEIVER_H
    
    #include 
    
    class Receiver : public QWidget
    {
        Q_OBJECT
    public:
        explicit Receiver(){};
        ~Receiver(){};
        void receive(const QString &msg);
    
    };
    
    #endif // RECEIVER_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    sender.cpp

    #include "sender.h"
    
    void Sender::send(const QString &name){
        emit message(name); // 使用emit发送信号
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    receiver.cpp

    #include "receiver.h"
    
    void Receiver::receive(const QString &name){
        qDebug()<< "received message:"<< name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    main.cpp

    #include "myslot.h"
    #include "sender.h"
    #include "receiver.h"
    
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Sender sender;//发送者
        Receiver receiver;//接收者
        QObject::connect(&sender,&Sender::message,&receiver,&Receiver::receive);//信号与槽函数的绑定
        sender.send("hello world");//发送信息
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    输出:

     received message: "hello world"
    
    • 1
    • 参与使用信号与槽机制通信的类都必须继承QObject类(即发送者与接收者都要继承QObject类),只有继承了QObject类的类才具备使用信号和槽机制通信的能。
    • 自定义的信号,它只有是一个函数声明,不需要实现,要求返回类型必须是void。
    • 使用emit发送信号
  • 相关阅读:
    python爬取网页信息
    【指针内功修炼】深度剖析指针笔试题(三)
    非零基础自学Java (老师:韩顺平) 第10章 面向对象编程(高级部分) 10.3 代码块
    关于IO口的一个没见过的坑
    小米 Civi 2 (ziyi) 机型解锁bl 获取root权限教程 +其他机型参数对比+救砖
    JS——利用js实现模态框的拖拽
    GlusterFS基本概念
    tp6使用rabb
    MotoGP Ignition:准备好参加 Spotlight 活动!
    论文基本功——LaTeX:公式及其编号
  • 原文地址:https://blog.csdn.net/weixin_40763897/article/details/126445565