信号和槽函数的扩展

news2024/9/23 14:28:48

信号和槽函数的扩展

    • 一个信号连接多个槽函数
    • 一个槽函数连接多个信号
    • 信号连接信号

一个信号可以连接多个槽函数, 发送一个信号有多个处理动作

  • 需要写多个connect()连接
  • 槽函数的执行顺序和信号的发射顺序相同(QT5中)
  • 信号的接收者可以是一个对象, 也可以是多个对象

一个槽函数可以连接多个信号, 多个不同的信号, 处理动作是相同的

  • 需要写多个connect()连接

信号可以连接信号

  • 信号接收者可以不处理接收的信号, 而是继续发射新的信号,这相当于传递了数据, 并没有对数据进行处理

一个信号连接多个槽函数

信号函数类

#ifndef ME_H
#define ME_H

#include <QObject>

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

signals:
    void sendMessage();   //信号函数不需要定义

};

#endif // ME_H

槽函数类:

#ifndef MEPARENT_H
#define MEPARENT_H

#include <QObject>

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

public slots:
    void receiveMessage();

};

#endif // MEPARENT_H

#include "meparent.h"
#include "QDebug"

meParent::meParent(QObject *parent) : QObject(parent)
{

}

void meParent::receiveMessage()
{
    qDebug()<<"父母接受到消息";
}

#ifndef MYFRIEND_H
#define MYFRIEND_H

#include <QObject>

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

public slots:
    void receiveMessage();

};

#endif // MYFRIEND_H

#include "myfriend.h"
#include "QDebug"
myFriend::myFriend(QObject *parent) : QObject(parent)
{

}

void myFriend::receiveMessage()
{
    qDebug()<<"朋友接受到消息";
}

#ifndef MYTEACHER_H
#define MYTEACHER_H

#include <QObject>

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

public slots:
    void receiveMessage();

};

#endif // MYTEACHER_H

#include "myteacher.h"
#include "QDebug"
myTeacher::myTeacher(QObject *parent) : QObject(parent)
{

}

void myTeacher::receiveMessage()
{
    qDebug()<<"老师接受到消息";
}

主窗口中,槽函数和类指针的声明

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"me.h"
#include"meparent.h"
#include "myfriend.h"
#include "myteacher.h"
#include "ouyang.h"
#include "shinecoln.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    me* tom;
    meParent* myparent;
    myFriend* myfriend;
    myTeacher* myteacher;
    ouyang* ou;
    shinecoln* shine;
//接受按钮信号的槽函数
public slots:
    void sendMessage();


private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

cpp文件如下:

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tom = new me(this);
    myparent = new meParent(this);
    myfriend = new myFriend(this);
    myteacher = new myTeacher(this);
    ou = new ouyang(this);
    shine = new shinecoln(this);
    //按钮绑定mainwindow的槽函数
    connect(ui->myPushButton,&QPushButton::clicked,this,&MainWindow::sendMessage);
    connect(tom,&me::sendMessage,myfriend,&myFriend::receiveMessage);
    connect(tom,&me::sendMessage,myparent,&meParent::receiveMessage);
    connect(tom,&me::sendMessage,myteacher,&myTeacher::receiveMessage);

}

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

void MainWindow::sendMessage()
{
    //qDebug()<<"hello";
    tom->sendMessage();
}


消息传递的流程:点击控件,触发主窗口的槽函数。主窗口槽函数调用tom->sendMessage();信号函数,myfriend,myfriend,myfriend同时捕获到信号,按照顺序调用myFriend::receiveMessage meParent::receiveMessage myTeacher::receiveMessage这三个槽函数。
在这里插入图片描述

一个槽函数连接多个信号

信号类:

#ifndef ME_H
#define ME_H

#include <QObject>

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

signals:
    void sendMessage();   //信号函数不需要定义
    void sendMsg2(QString);

};

#endif // ME_H

#ifndef OUYANG_H
#define OUYANG_H

#include <QObject>

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

signals:
    void sendMessage();  //信号函数不需要定义
    void sendMsg2(QString);
};

#endif // OUYANG_H


#ifndef SHINECOLN_H
#define SHINECOLN_H

#include <QObject>

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

signals:
    void sendMessage();  //信号函数不需要定义
    void sendMsg2(QString);
};

#endif // SHINECOLN_H

槽函数类:

#ifndef MEPARENT_H
#define MEPARENT_H

#include <QObject>

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

public slots:
    void receiveMessage();
    void receiveMsg2(QString msg);

};

#endif // MEPARENT_H

#include "meparent.h"
#include "QDebug"

meParent::meParent(QObject *parent) : QObject(parent)
{

}

void meParent::receiveMessage()
{
    qDebug()<<"父母接受到消息";
}

void meParent::receiveMsg2(QString msg)
{
    qDebug()<<"父母接受到: "<<msg;
}

ui页面:
在这里插入图片描述

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"me.h"
#include"meparent.h"
#include "myfriend.h"
#include "myteacher.h"
#include "ouyang.h"
#include "shinecoln.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    me* tom;
    meParent* myparent;
    myFriend* myfriend;
    myTeacher* myteacher;
    ouyang* ou;
    shinecoln* shine;
//接受按钮信号的槽函数
public slots:
    void sendMessage();
    void sendMsg2();
    void sendMsg3();
    void sendMsg4();


private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

cpp文件:

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tom = new me(this);
    myparent = new meParent(this);
    myfriend = new myFriend(this);
    myteacher = new myTeacher(this);
    ou = new ouyang(this);
    shine = new shinecoln(this);
    //一个信号绑定多个槽函数
    //按钮绑定mainwindow的槽函数
    connect(ui->myPushButton,&QPushButton::clicked,this,&MainWindow::sendMessage);
    connect(tom,&me::sendMessage,myfriend,&myFriend::receiveMessage);
    connect(tom,&me::sendMessage,myparent,&meParent::receiveMessage);
    connect(tom,&me::sendMessage,myteacher,&myTeacher::receiveMessage);


    connect(ui->myPushButton2,&QPushButton::clicked,this,&MainWindow::sendMsg2);
    connect(ui->myPushButton3,&QPushButton::clicked,this,&MainWindow::sendMsg3);
    connect(ui->myPushButton4,&QPushButton::clicked,this,&MainWindow::sendMsg4);
    //一个槽函数绑定多个信号
    connect(tom,&me::sendMsg2,myparent,&meParent::receiveMsg2);
    connect(ou,&ouyang::sendMsg2,myparent,&meParent::receiveMsg2);
    connect(shine,&shinecoln::sendMsg2,myparent,&meParent::receiveMsg2);

}

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

void MainWindow::sendMessage()
{
    //qDebug()<<"hello";
    tom->sendMessage();
}

void MainWindow::sendMsg2()
{
    QString s = "tom";
    tom->sendMsg2(s);
}

void MainWindow::sendMsg3()
{
    QString s = "ou";
    ou->sendMsg2(s);
}

void MainWindow::sendMsg4()
{
    QString s = "shine";
    shine->sendMsg2(s);
}


关键代码:
在这里插入图片描述
在这里插入图片描述

运行:
在这里插入图片描述

信号连接信号

信号接收者可以不处理接收的信号, 而是继续发射新的信号,这相当于传递了数据, 并没有对数据进行处理
槽函数的位置不对信号处理,而是对信号的转发。

connect(const QObject *sender, &QObject::signal,  const QObject *receiver, &QObject::siganl-new);

例子:

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tom = new me(this);
    myparent = new meParent(this);
    myfriend = new myFriend(this);
    myteacher = new myTeacher(this);
    ou = new ouyang(this);
    shine = new shinecoln(this);
    //一个信号绑定多个槽函数
    //按钮绑定mainwindow的槽函数
    connect(ui->myPushButton,&QPushButton::clicked,this,&MainWindow::sendMessage);
    connect(tom,&me::sendMessage,myfriend,&myFriend::receiveMessage);
    connect(tom,&me::sendMessage,myparent,&meParent::receiveMessage);
    connect(tom,&me::sendMessage,myteacher,&myTeacher::receiveMessage);


    connect(ui->myPushButton2,&QPushButton::clicked,this,&MainWindow::sendMsg2);
    connect(ui->myPushButton3,&QPushButton::clicked,this,&MainWindow::sendMsg3);
    connect(ui->myPushButton4,&QPushButton::clicked,this,&MainWindow::sendMsg4);
    //一个槽函数绑定多个信号
    connect(tom,&me::sendMsg2,myparent,&meParent::receiveMsg2);
    connect(ou,&ouyang::sendMsg2,myparent,&meParent::receiveMsg2);
    connect(shine,&shinecoln::sendMsg2,myparent,&meParent::receiveMsg2);

    //信号连接信号
    connect(ui->myPushButton5,&QPushButton::clicked,tom,&me::sendMessage);

}

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

void MainWindow::sendMessage()
{
    //qDebug()<<"hello";
    tom->sendMessage();
}

void MainWindow::sendMsg2()
{
    QString s = "tom";
    tom->sendMsg2(s);
}

void MainWindow::sendMsg3()
{
    QString s = "ou";
    ou->sendMsg2(s);
}

void MainWindow::sendMsg4()
{
    QString s = "shine";
    shine->sendMsg2(s);
}


在这里插入图片描述
新建的是 myPushButton5 按钮:

    //信号连接信号
    connect(ui->myPushButton5,&QPushButton::clicked,tom,&me::sendMessage);

信号连接信号(之前都是在mainwindow中建立了槽函数,然后槽函数发送信号)

现在就直接点击按钮,发送信号,然后触发第二个信号me::sendMessage

之前的代码捕获到信号

运行结果为:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/807280.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C#实现读写CSV文件的方法详解

目录 CSV文件标准 文件示例RFC 4180简化标准读写CSV文件 使用CsvHelper使用自定义方法总结 项目中经常遇到CSV文件的读写需求&#xff0c;其中的难点主要是CSV文件的解析。本文会介绍CsvHelper、TextFieldParser、正则表达式三种解析CSV文件的方法&#xff0c;顺带也会介绍一…

SpringBoot中MongoDB的使用

SpringBoot中MongoDB的使用 MongoDB 是最早热门非关系数据库的之一&#xff0c;使用也比较普遍&#xff0c;一般会用做离线数据分析来使用&#xff0c;放到内网的居 多。由于很多公司使用了云服务&#xff0c;服务器默认都开放了外网地址&#xff0c;导致前一阵子大批 MongoD…

骨传导耳机是什么?为什么不用塞到耳朵里?

骨传导耳机其实就跟它的名字一样&#xff0c;用骨传导声音的耳机&#xff0c;整个声音传导过程都是开放双耳的&#xff0c;不接触耳膜&#xff0c;佩戴非常舒适的耳机。 为什么不需要塞进耳朵里&#xff0c;首先咱们要先知道骨传导的原理&#xff1a; 如上图所示&#xff0c;骨…

【Linux命令200例】less强大的文件内容查看工具

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;全栈领域新星创作者✌&#xff0c;2023年6月csdn上海赛道top4。 &#x1f3c6;本文已收录于专栏&#xff1a;Linux命令大全。 &#x1f3c6;本专栏我们会通过具体的系统的命令讲解加上鲜活的实操案例对各个命令进行深入…

项目实战 — 消息队列(1) {需求分析}

一、什么是消息队列 消息队列&#xff08;Message Queue ,MQ&#xff09;&#xff0c;就是将阻塞队列的数据结构&#xff0c;提取成了一个程序&#xff0c;独立进行部署。也就是实现一个生产者消费模型。 有关生产者消费者模型&#xff0c;参考多线程 — 阻塞队列_多线程阻塞…

redis基本架构:一个键值数据库包含什么?(这篇文章主要是一个引导的作用)

我们设计一个简单的smpliekv数据库&#xff0c;来体验简直数据库包含什么 体来说&#xff0c;一个键值数据库包括了访问框架、索引模块、操作模块和存储模块四部分&#xff08;见 下图&#xff09;。接下来&#xff0c;我们就从这四个部分入手&#xff0c;继续构建我们的 Simpl…

【MyBatis】MyBatis 3.5+版本报错合集(持续更新)

报错&#xff1a;BindingException 1. org.apache.ibatis.binding.BindingException: Type interface xxx is not known to the MapperRegistry. 2. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx 解决方案 在pom.xml中添加如下代码…

NetApp FAS控制器的启动过程详解

了解NetApp FAS存储系统的控制器启动过程对于控制器故障的诊断分析非常重要&#xff0c;最近在给几个小朋友培训NetApp&#xff0c;顺便把这个启动过程总结了一下&#xff0c;分享给大家&#xff0c;遇到控制器故障&#xff0c;就可以照方抓药了&#xff0c;如果还是搞不懂&…

自学网络安全(黑客)的注意事项

自学网络安全是一项重要而复杂的任务&#xff0c;以下是一些注意事项&#xff1a; 确定学习目标&#xff1a;网络安全是一个广泛的领域&#xff0c;包括密码学、网络防御、漏洞利用等多个方面。在自学之前&#xff0c;确定你感兴趣的领域&#xff0c;并设定明确的学习目标。 寻…

自动化测试如何管理测试数据

前段时间&#xff0c;知识星球里有同学问到&#xff1a;自动化case越多&#xff0c;测试数据越多&#xff0c;数据的管理成本也越来越高&#xff0c;是否需要一个数据池来专门管理测试数据&#xff1f;这是一个好问题&#xff0c;也是很多测试同学在自动化测试实践中必须面对的…

RWEQ模型土壤风蚀模拟与风蚀模数估算、数据支持、参量提取、归因分析、相关SCI论文撰写技巧

目录 专题一 理论基础 专题二 平台基础 专题三 RWEQ模型数据支持 专题四 RWEQ模型参量提取 专题五 归因分析 专题六 RWEQ模型相关的SCI论文撰写技巧 结合案例讲解RWEQ模型的运行及相关的归因分析&#xff0c;从原理、数据、方法、归因分析方面对土壤风蚀情况进行实战讲解…

绝不多花一分钱,IT老兵的云上省钱之旅

相信很多网友感觉今年的日子不好过&#xff0c;各方面都在缩减支出&#xff0c;尤其是部分IT设备还在不断涨价&#xff0c;像今年的英伟达的40系桌面级显卡和A/H系列的商用显卡&#xff0c;动辙价格跳涨30%&#xff0c;让广大开发者苦不堪言。所幸在省钱方面&#xff0c;笔者有…

【c++底层结构】AVL树红黑树

【c底层结构】AVL树&红黑树 1.AVL树1.1 AVL树的概念1.2 AVL树结点的定义1.3 AVL树的插入1.4 AVL树的旋转1.5 AVL树的验证1.6 AVL树的性能 2. 红黑树2.1 红黑树的概念2.2 红黑树的性质2.3 红黑树节点的定义2.4 红黑树的插入操作2.5 红黑树的验证2.6 红黑树与AVL树的比较2.7 …

linux之iptables的理解与使用

1. 前言 iptables是一个用于Linux操作系统的防火墙软件&#xff0c;它可以对网络流量进行过滤、修改和重定向&#xff0c;从而控制网络通信。iptables是Linux内核中的一个子系统&#xff0c;它可以通过在命令行输入规则来配置网络防火墙。iptables可以对入站和出站的流量进行控…

初识mysql数据库之事务的隔离性

目录 一、理解隔离性 二、隔离级别 1. 不同的隔离级别的简单概述 2. 查看隔离级别 2.1 查看全局隔离级别 2.2 查看会话隔离级别 3. 设置隔离界别 4. 读未提交&#xff08;Read Uncommitted&#xff09; 4.1 读未提交测试 5. 读提交&#xff08;Read Committed&#x…

Windows 10 安装 PostgreSQL 12.x 报错 ‘psql‘ 不是内部或外部命令 由于找不到文件libintl-9.dll等问题

目录 序言一、问题总结问题 1 psql 不是内部或外部命令&#xff0c;也不是可运行的程序或批处理文件。问题 2 “由于找不到文件libintl-9.dll&#xff0c;无法继续执行代码&#xff0c;重新安装程序可能会解决此问题。“1、卸载2、安装3、安装 Stack Builder &#xff08;这个可…

Easyexcel简介及写、读操作

Easyexcel简介及写、读操作 一、背景二、简介三、引入依赖四、代码实现1.创建实体类2.写入excel操作3.读取文件操作3.1 指定excel对应索引3.2 设置监听器3.3 执行读取操作 一、背景 作为一个经常进行数据分析的后端人员&#xff0c;免不了面对各种报表&#xff0c;且在日常的工…

onTouchEvent浅析

我们接着上次的自定义星星来作讲解 当 onTouchEvent 返回 super.onTouchEvent ( false ) 时 public boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Log.d("ACTION_DOWN","ACTION_DOWN:"event.getA…

Ansible-playbook(剧本)

Ansible-playbook(剧本) 一、playbook的构成 &#xff08;1&#xff09;Tasks&#xff1a;任务&#xff0c;即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行&#xff08;2&#xff09;Variables&#xff1a;变量&#xff08;3&#xff09;Templates&am…

mybatis复杂环境搭建-多对一的处理-一对多的处理

复杂环境搭建&#xff1a; 1.1建表&#xff1a; CREATE table teacher( id int(10) not null, name varchar(30) default null, primary key(id) )engineInnoDB default charsetutf8mb3;INSERT INTO teacher (id, name) VALUES (1, 何老师);create table student( id int(10)…