直播相关02-录制麦克风声音,QT 信号与槽,自定义信号和槽

一 信号与槽函数

#include "mainwindow.h"
#include <QPushButton>
#include <iostream>
using namespace std;

//我们的目的是在 window中加入一个button,当点击这个button后,关闭 MainWindow 。
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //注意,这里是new 了一个 QpushButton的,那么理论上要 delete 这个loginButton 。这里为什么 没有呢?
    QPushButton * loginButton = new QPushButton();
    loginButton->setText("登陆");
    loginButton->setParent(this); // 将自己挂载到当前的 mianwindow上,这意味着,当 mainwindows 销毁的时候,loginbutton 会跟着销毁,因此不用手动的调用 elete loginbutton

    loginButton->setFixedSize(100,30); // 设置button 为固定大小

    //我们将 loginbutton 的 clicked信号,给MainWindow 的 close槽函数。
    //qpushbutton 的clicked 信号,是qpushbutton 自带的信号
    //mainwindows 的close 槽函数,是 mainwindows 自带的槽函数
    connect(loginButton,&QPushButton::clicked,this,&MainWindow::close);

}

MainWindow::~MainWindow()
{
}

二 自定义信号和 自定义 槽函数

1.自定义信号,添加一个C++ 类,要继承 QObject

主要你要自定义信号与槽,就一定要 Add Q_OBJECT

sender.h 文件

#ifndef SENDER_H
#define SENDER_H

#include <QObject>

class Sender : public QObject
{
    //主要你要自定义信号与槽,就一定要 Add Q_OBJECT,这个取消了会有问题
    Q_OBJECT
public:
    explicit Sender(QObject *parent = nullptr);

signals:
    //自定义信号,只需要在.h文件中声明就可以了,不需要在.cpp中实现
    void exit();
    void start();
};

#endif // SENDER_H

2.自定义槽函,添加一个C++类,要继承 QObject

在receiver.h 中定义这两个槽函数

在receiver.cpp中实现这两个槽函数

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>

class receiver : public QObject
{
    //主要你要自定义信号与槽,就一定要 Add Q_OBJECT,这个取消了会有问题
    Q_OBJECT
public:
    explicit receiver(QObject *parent = nullptr);

signals:

    //槽函数是要给别人调用的,因此在public slots 后 写 槽函数,在.cpp文件中写实现.
public slots:
    void handleExit();
    void handleStart();


};

#endif // RECEIVER_H

#include "receiver.h"
#include <iostream>
using namespace std;

receiver::receiver(QObject *parent) : QObject(parent)
{

}


void receiver::handleExit(){

    cout<<"receiver handleExit method "<<endl;
}
void receiver::handleStart(){
    cout<<"receiver handleStart method "<<endl;
}

3.调用

这里为了方便期间,将上述两个类 重命名为 customerreceiver.h   和  customersender.h

    //将信号发送者 和 信号 接受者 建立关联
    //使用emit 发送信号, 实际上就是 调用 发送者的信号函数
 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "customersender.h"
#include "customerreceiver.h"
#include <iostream>
using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    cout<<"qt custom single custom slot"<<endl;
    ui->setupUi(this);
    CustomerSender * customersender = new CustomerSender;

    CustomerReceiver * customerrecevier =  new CustomerReceiver();

    //将信号发送者 和 信号 接受者 建立关联
    connect(customersender,&CustomerSender::start,
            customerrecevier,&CustomerReceiver::handleStart);

    connect(customersender,&CustomerSender::exit,
            customerrecevier,&CustomerReceiver::handleExit);

    cout<<"00000 log for watch"<<endl;

    //使用emit 发送信号, 实际上就是 调用 发送者的信号函数
    emit customersender->start();
    cout<<"11111log for watch"<<endl;
    emit customersender->exit();
    cout<<"222"<<endl;

    //由于 customersender 和 customerrecevier 并没有挂载到当前mainwindows上,类似这样customersender->setParent(this);因此要手动delete
    delete customersender;
    delete customerrecevier;
}

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

4. 当自定义信号 和 自定义槽函数 有参数或者返回值的时候

自定义信号 addmethodstart ,参数为 int a 和 int b

返回值double 目前不知道有啥用

signals:
    //自定义信号,只需要在.h文件中声明就可以了,不需要在.cpp中实现
    void exit();
    void start();

    double addmethodstart(int a ,int b );

自定义槽函数

    int handleAddFuncation();

    double handleAddFuncation2(int n , int m);




int CustomerReceiver::handleAddFuncation(){
    cout<<"CustomerReceiver handleAddFuncation method return 100"<<endl;
    return 100;
}

double CustomerReceiver::handleAddFuncation2(int n , int m){
    cout<<"CustomerReceiver handleAddFuncation2 method return n+m "<< n+m << endl;
    return n+m;
}

当我们发送一个信号的时候,如果有槽函数有对应的 int,int 参数,则会接受 信号发送的2000,3000的值传递到槽函数中。

如果槽函数没有对应的int,int 参数,则不传递

    connect(customersender,&CustomerSender::addmethodstart,
            customerrecevier,&CustomerReceiver::handleAddFuncation);
    cout<< emit customersender->addmethodstart(200,300);//由于 handleAddFuncation 函数没有参数,因此这个200,300传递不到槽函数handleAddFuncation中
    cout<<"333"<<endl;

    connect(customersender,&CustomerSender::addmethodstart,
            customerrecevier,&CustomerReceiver::handleAddFuncation2);
    cout<< emit customersender->addmethodstart(2000,3000);//由于handleAddFuncation2 有int,int 的参数 ,因此这个2000,3000会传递给槽函数 handleAddFuncation2
   

返回值问题,信号的返回值目前没有发现意义是啥,

槽函数的返回值 可以通过 emit 信号函数返回。

例如:

cout<< emit customersender->addmethodstart(200,300);
会打印 100出来,这是因为 对应的槽函数 handleAddFuncation 返回值是100

5. 连接多个信号

connect 函数除了可以连接一个 信号和一个槽函数外,

还可以连接一个信号 和 另一个信号 .

类似于连锁反应

customersender2.h

#ifndef CUSTOMERSENDER2_H
#define CUSTOMERSENDER2_H

#include <QObject>

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

signals:

    void deletebutton2();
};

#endif // CUSTOMERSENDER2_H

customersender3.h

#ifndef CUSTOMERSENDER3_H
#define CUSTOMERSENDER3_H

#include <QObject>

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

signals:

    void deletebutton3();
};

#endif // CUSTOMERSENDER3_H

customerreceuver2.h
#ifndef CUSTOMERRECEUVER2_H
#define CUSTOMERRECEUVER2_H

#include <QObject>

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

signals:


public slots:
        void handledeletebutton2();
};

#endif // CUSTOMERRECEUVER2_H

customerreceuver3.h

#ifndef CUSTOMERRECEUVER3_H
#define CUSTOMERRECEUVER3_H

#include <QObject>

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

signals:


public slots:
    void handledeletebutton3();
};

#endif // CUSTOMERRECEUVER3_H

customerreceuver2.cpp
#include "customerreceuver2.h"
#include <iostream>
using namespace  std;

customerreceuver2::customerreceuver2(QObject *parent) : QObject(parent)
{

}


void customerreceuver2::handledeletebutton2(){

    cout<<"customerreceuver2::handledeletebutton2"<<endl;
}

customerreceuver3.cpp

#include "customerreceuver3.h"
#include <iostream>
using namespace std;

customerreceuver3::customerreceuver3(QObject *parent) : QObject(parent)
{

}

void customerreceuver3::handledeletebutton3(){
    cout<<"customerreceuver3::handledeletebutton3"<<endl;

}

调用

        cout<<" single to single"<<endl;
    customersender2 *cus2 = new customersender2;
    customersender3 *cus3 = new customersender3;

    customerreceuver2 *cusre2 =  new customerreceuver2;
    customerreceuver3 *cusre3 =  new customerreceuver3;

    connect(cus2, &customersender2::deletebutton2,
            cusre2,&customerreceuver2::handledeletebutton2);

    emit cus2->deletebutton2();

    cout<<"111"<<endl;

    connect(cus3, &customersender3::deletebutton3,
            cusre3,&customerreceuver3::handledeletebutton3);

    emit cus3->deletebutton3();
    cout<<"222 "<<endl;


    cout<<"after this line will be test single to single"<<endl;

    //让 sender2 的信号deletebutton2 唤醒 sender3 的信号 deletebutton3
    connect(cus2, &customersender2::deletebutton2,
            cus3, &customersender3::deletebutton3);

    emit cus2->deletebutton2();
    cout<<"333333"<<endl;
    
//    single to single
//    customerreceuver2::handledeletebutton2
//    111
//    customerreceuver3::handledeletebutton3
//    222 
//    after this line will be test single to single
//    customerreceuver2::handledeletebutton2
//    customerreceuver3::handledeletebutton3
//    333333

6.labmda 表达式

    //for test labmda,假设我们不想写receiver

    connect(cus2, &customersender2::deletebutton2,[](){
        cout<<"labmda called"<<endl;
    });
    emit cus2->deletebutton2();
    cout<<"666666"<<endl;

三 当QT使用 genetate form 创建UI后,如何使用 信号和槽函数呢?

也就是说,我们使用了QT 的 generate form 来创建UI

我们在弄了两个pushbutton,一个登陆,一个注册,可以观察到 QT会给key 命名为 pushButton,一个为pushButton_2,当然可以改动这个key的值,

我们这里重新命名为 loginbutton和registerbutton

检查 编辑 下的 UI 的button,如下:

那么我们想要给这两个button 添加 信号和槽事件应该怎么弄呢?

点击 "转到槽" 做了两件事情

1. 点击那个信号  相当于 选择了那个 信号

2.然后生成 该信号 对应的 槽函数

例如我们点击了 clicked() 信号,就会生成  void on_loginbutton_clicked() 参函数

 

槽函数的位置

那么 emit 是在什么时候调用呢?实际上我们是看不到的,QT内部帮我们实现了,只要我们完成了上述的 button 的 转到槽函数, 就可以了

测试:

手动的写 槽函数,而不是通过自动生成

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

速通GPT-3:Language Models are Few-Shot Learners全文解读

文章目录 GPT系列论文速通论文实验总览1. 任务设置与测试策略2. 任务类别3. 关键实验结果4. 数据污染与实验局限性5. 总结与贡献 Abstract1. 概括2. 具体分析3. 摘要全文翻译4. 为什么不需要梯度更新或微调⭐ Introduction1. 概括2. 具体分析3. 进一步分析 Approach1. 概括2. 具…

【Unity学习心得】如何制作俯视角射击游戏

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、导入素材二、制作流程 1.制作地图2.实现人物动画和移动脚本3.制作单例模式和对象池4.制作手枪pistol和子弹bullet和子弹壳bulletShell5.制作散弹枪shotgun总…

MMLU-Pro 基准测试数据集上线,含 12k 个跨学科复杂问题,难度提升,更具挑战性!DeepSeek 数学模型一键部署

在大语言模型 (LLM) 蓬勃发展的时代&#xff0c;诸如大规模多任务语言理解 (MMLU) 之类的基准测试&#xff0c;在推动 AI 于不同领域的语言理解与推理能力迈向极限方面&#xff0c;发挥着至关重要的关键作用。 然而&#xff0c;伴随模型的持续改进与优化&#xff0c;LLM 在这些…

Vue路由:Vue router

目录 路由的基本概念 1. 路由 2. 单页应用SPA 3.前端路由的实现方式 3.1Hash模式 3.2History模式 Vue router 4 1.概述 2.安装使用 3.基础用法 3.1路由匹配规则声明 3.2动态路由匹配 3.3路由命名 3.4路由重定向 3.5路由嵌套 3.6命名视图 3.6声明式导航&编程…

el-input设置type=‘number‘和v-model.number的区别

el-input设置typenumber’与设置.number修饰符的区别 1. 设置type‘number’ 使用el-input时想收集数字类型的数据&#xff0c;我们首先会想到typenumber&#xff0c;设置完type为number时会限制我们输入的内容只能为数字&#xff0c;不能为字符/汉字等非数字类型的数值&…

【网络安全】-文件下载漏洞-pikachu

文件操作漏洞包括文件上传漏洞&#xff0c;文件包含漏洞&#xff0c;文件下载漏洞。 文章目录  前言 什么是文件下载漏洞&#xff1f; 1.常见形式&#xff1a; 常见链接形式&#xff1a; 常见参数&#xff1a; 2.利用方式&#xff1a; 3.举例&#xff1a;pikachu不安全的文件…

智能语音技术在人机交互中的应用与发展

摘要&#xff1a;本文主要探讨智能自动语音识别技术与语音合成技术在构建智能口语系统方面的作用。这两项技术实现了人机语音通信&#xff0c;建立起能听能说的智能口语系统。同时&#xff0c;引入开源 AI 智能名片小程序&#xff0c;分析其在智能语音技术应用场景下的意义与发…

使用ESP8266和OLED屏幕实现一个小型电脑性能监控

前言 最近大扫除&#xff0c;发现自己还有几个ESP8266MCU和一个0.96寸的oled小屏幕。又想起最近一直想要买一个屏幕作为性能监控&#xff0c;随机开始自己diy。 硬件&#xff1a; ESP8266 MUColed小屏幕杜邦线可以传输数据的数据线 环境 Windows系统Qt6Arduino Arduino 库…

计算架构模式之负载均衡技巧

通用负载均衡算法 负载均衡算法 -轮询 & 随机 如果服务器挂掉了&#xff0c;那么负载均衡器还是可以感知到的&#xff0c;因为连接已经断掉了。 负载均衡算法-加权轮询 假设你有4核的和8核的&#xff0c;由于你的程序没有办法跑完CPU&#xff0c;那么有可能出现4核的和8核…

Coggle数据科学 | 科大讯飞AI大赛:人岗匹配挑战赛 赛季3

本文来源公众号“Coggle数据科学”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;科大讯飞AI大赛&#xff1a;人岗匹配挑战赛 赛季3 赛题名称&#xff1a;人岗匹配挑战赛 赛季3 赛题类型&#xff1a;自然语言处理、文本匹配 赛题…

Pikachu靶场之csrf

CSRF 跨站请求伪造 CSRF入门及靶场实战 - FreeBuf网络安全行业门户 攻击者伪造恶意链接&#xff0c;诱使用户点击&#xff0c;这个链接附带了用户的认证凭据Cookie、Session等&#xff0c;执行操作如转账。 因为带了cookie、session&#xff0c;服务器认为是用户的行为。借用…

【诉讼流程-健身房-违约认定-私教课-诉讼书前提材料整理-民事诉讼-自我学习-铺平通往法律的阶梯-讲解(2)】

【诉讼流程-健身房-违约-私教课-前期法律流程-民事诉讼-自我学习-铺平通往法律的阶梯-讲解&#xff08;2&#xff09;】 &#xff08;1&#xff09;前言说明1、目的2、一个小测试1、更换原教练2、频繁更换教练3、上课估计拖课&#xff0c;占用上课时间&#xff0c;抽烟等。4、以…

谈谈LLM训练中的“过拟合”与“欠拟合”

如今&#xff0c;由于其出色的理解、生成和操纵人类语言的能力&#xff0c;语言模型已经成为焦点。据最新调查数据显示&#xff0c;大概30%的企业计划使用非结构化数据来提高大型语言模型&#xff08;LLM&#xff09;的准确性。在训练这些语言模型时&#xff0c;一个基本挑战是…

知识笔记合集

文章目录 vsCode可以运行c程序却无法运行c程序帆软填报属性不起作用java-实体类日期类型格式化Java-数据库id字段使用雪花算法IDEA-快捷键 vsCode可以运行c程序却无法运行c程序 vsCode中的tasks.json文件中添加"-lstdc" {"tasks": [{"type": &…

【vuetify】v-select 无法正常显示,踩坑记录!

一、上代码 template <v-selectv-model"editedUser.userRole":items"roles"label"角色"item-value"value":rules"[rules.required]" ></v-select>script const editedUser ref({userRole: customer // 设置…

【LabVIEW学习篇 - 21】:DLL与API的调用

文章目录 DLL与API调用DLLAPIDLL的调用 DLL与API调用 LabVIEW虽然已经足够强大&#xff0c;但不同的语言在不同领域都有着自己的优势&#xff0c;为了强强联合&#xff0c;LabVIEW提供了强大的外部程序接口能力&#xff0c;包括DLL、CIN(C语言接口)、ActiveX、.NET、MATLAB等等…

利用 Zero-1-2-3 进行多视图 3D 重建:从单图像到多视图 3D 模型的生成

3D 模型生成在计算机视觉领域有着广泛的应用&#xff0c;从虚拟现实到自动驾驶&#xff0c;基于单张图像的 3D 重建技术正在迅速发展。这篇博客将带你深入探索如何使用 Zero-1-2-3 框架进行多视图 3D 重建&#xff0c;通过详细解析该框架中的代码结构和功能&#xff0c;帮助你理…

MFC工控项目实例之十五定时刷新PC6325A模拟量输入

承接专栏《MFC工控项目实例之十四模拟量信号名称从文件读写》 1、在BoardTest.h文件中添加代码 class CBoardTest : public CDialog { public:short m_saveData[32];unsigned short m_cardAddr;CBoardTest(CWnd* pParent NULL); // standard constructorCButtonST m_btnS…

【新时代概论】新时代概论书目的结构(LP)

文章目录 前言一、结构导论第一章、新时代坚持和发展中国特色社会主义第二章、以中国式现代化全面推进中华民族伟大复兴第三章、坚持党的全面领导第四章、坚持以人民为中心第五章、全面深化改革开放第六章、推动高质量发展第七章、社会主义现代化建设的教育、科技、人才战略第八…

海外云手机怎么实现TikTok多账号防关联?

TikTok多账号运营&#xff0c;作为众多用户选择的引流策略&#xff0c;旨在通过多账号的协同作用&#xff0c;更快速、高效地推动主账号的流量增长。然而&#xff0c;这一策略面临着一个关键难题——TikTok账号防关联。本文将简要介绍海外云手机如何解决这一问题。 在TikTok多账…