直播相关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 loginbuttonloginButton->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);因此要手动deletedelete 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 的信号 deletebutton3connect(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,假设我们不想写receiverconnect(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.mzph.cn/news/879187.shtml

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

相关文章

0.3 学习Stm32经历过的磨难

文章目录 用库函数传参 能否按位或STM32库函数XXX_GetFlagStatus和XXX_GetITStatus的区别关于MDK导入文件后报错 Browse information of one files is not available用exti中断读取按键 忘记消抖 &#xff08;更离谱的是&#xff0c;我忘记开启afio的时钟了 Damn!&#xff09;D…

【Lua学习】Lua入门

上一篇帖子【Lua学习】Lua最最基础的 – 经云的清净小站 (skycreator.top)讲了Lua是什么&#xff0c;Lua如何安装在Linux和Windows上。那么安装好之后&#xff0c;我们就要使用Lua实现我们的各种功能了。 首先&#xff0c;我们要先了解Lua一些最基本的内容&#xff0c;比如怎么…

嵌入式QT学习第1天:C++基础

Linux版本号4.1.15 芯片I.MX6ULL 大叔学Linux 品人间百味 思文短情长 1.输入输出方式&#xff1a;cin cout cout<<x<<endl;//endl是换行符 2.命名空间&#xff1a;无.h的是标准输入输出流&#xff0c;要用命名空间。 #inc…

攻防世界 Web_php_unserialize

Web_php_unserialize PHP反序列化 看看代码 <?php class Demo { private $file index.php;public function __construct($file) { $this->file $file; }function __destruct() { echo highlight_file($this->file, true); }function __wakeup() { if ($this->…

【QT】自制一个简单的时钟(跟随系统时间)

目录 源代码&#xff1a; 输出结果如下&#xff1a; 使用QT完成一个简单的时钟图形化界面&#xff0c;功能是完成了时分秒指针能够跟随系统时间移动 设计思路&#xff1a; 1、首先将时钟的边框绘制出来 2、定义出一个定时器t1&#xff0c;将定时器连接到update_slot槽内&#…

【Kubernetes知识点问答题】资源配额 / 访问控制

目录 1. 解释 ResourceQuota 的作用。 2. 解释 Service Account 的用途。 3. 详细解释 Role 和 ClusterRole。 4. 什么是 K8s 的 NetworkPolicy&#xff1f; 5. 详细描述在 K8s 中如何控制跨 Namespace 的 Pod 访问&#xff1f; 1. 解释 ResourceQuota 的作用。 - Resource…

基于SpringBoot的大学生创新创业训练项目管理系统

文章目录 项目介绍主要功能截图:部分代码展示设计总结项目获取方式🍅 作者主页:超级无敌暴龙战士塔塔开 🍅 简介:Java领域优质创作者🏆、 简历模板、学习资料、面试题库【关注我,都给你】 🍅文末获取源码联系🍅 项目介绍 基于SpringBoot的大学生创新创业训练项目…

supervisor安装CeSi集中化管理Supervisor

一、安装supervisor 备注&#xff1a;supervisor 只能管理前台进程的服务&#xff0c;比如 npm run 这些 &#xff0c;一些后台运行的服务无法管理【后台运行的服务可以用systemd 进行管理】 1、安装epel源 yum install epel-release yum install -y supervisor 2、创建sup…

hint: See PEP 668 for the detailed specification.

出现 externally-managed-environment 错误的原因是你的操作系统或 Python 环境开启了 PEP 668 保护机制。即使你创建了新的 conda 虚拟环境&#xff0c;系统仍然标记该环境为“外部管理的”&#xff0c;不允许直接通过 pip 安装包。这是为了保护系统级的 Python 环境不被破坏。…

比较stl库的ostringstream与Qt的QString::arg(),QString::number()

需求&#xff1a; 显示一个float或者double类型的数&#xff0c;要求小数点后的数字位数为定值。 考虑STL库的ostringstream或者Qt的QString::arg(), number 对于stringstream,使用比较繁琐&#xff0c;要联合使用std::fixed和std::setprecision才能实现固定小数位数显示&am…

[论文笔记]QLoRA: Efficient Finetuning of Quantized LLMs

引言 今天带来LoRA的量化版论文笔记——QLoRA: Efficient Finetuning of Quantized LLMs 为了简单&#xff0c;下文中以翻译的口吻记录&#xff0c;比如替换"作者"为"我们"。 我们提出了QLoRA&#xff0c;一种高效的微调方法&#xff0c;它在减少内存使用…

C语言深入理解指针五(18)

文章目录 前言一、回调函数是什么&#xff1f;二、qsort使用举例使用qsort函数排序整型数据使用qsort函数排序结构数据 三、qsort的模拟实现总结 前言 本篇将会很有意思&#xff01; 一、回调函数是什么&#xff1f; 回调函数就是一个通过函数指针调用的函数。   如果你把函数…

C++——STL——栈(stack)

栈的定义 栈 &#xff08; stack &#xff09;是限定仅在表的一端进行插入和删除操作的线性表&#xff0c;允许插入和删除的一端称 为栈顶&#xff0c;另一端称为栈底&#xff0c;不含任何数据元素的栈称为空栈。 栈的示意图 因为栈只能够在一端进行插入和删除&#xff0c;所以…

大数据之Flink(三)

9.3、转换算子 9.3.1、基本转换算子 9.3.1.1、映射map 一一映射 package transform;import bean.WaterSensor; import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; impor…

鸿蒙交互事件开发04——手势事件

1 概 述 手势事件是移动应用开发中最常见的事件之一&#xff0c;鸿蒙提供了一些方法来绑定手势事件。通过给各个组件绑定不同的手势事件&#xff0c;并设计事件的响应方式&#xff0c;当手势识别成功时&#xff0c;ArkUI框架将通过事件回调通知组件手势识别的结果。 …

王道考研操作系统笔记(一)

虚拟内存的定义和特征&#xff1a; 基于局部性的原理&#xff0c; 在程序装入时&#xff0c;可以将程序中很快用到的部分装入内存&#xff0c;暂时用不到的数据装入外存&#xff0c;就可以让程序开始执行&#xff0c;在程序执行过程中&#xff0c;当所访问的信息不在内存的时…

frida主动调用init_array中的函数

ida打开目标so&#xff0c;查看要主动调用的函数 前提是先过掉检测frida等等&#xff0c;然后控制台启动 输出so地址 Process.findModuleByName("libmod.so") New函数 var aa new NativeFunction(ptr(0x785e002000).add(0x134EC0),"void",[]) 主动调用 a…

如何让人工智能训练更快

影响人工智能训练时间的因素 在深度学习训练中&#xff0c;训练时间的计算涉及到多个因素&#xff0c;包括 epoch 数、全局 batch size、微 batch size、计算设备数量等。下面是一个基本的公式来说明这些参数之间的关系&#xff08;注意&#xff0c;这只是一个基本的说明公式&…

Makefile文件理解

https://zhuanlan.zhihu.com/p/629855009 参考链接 这个链接我没都看&#xff0c;等用的时候再看吧 我遇到的文件是下面这张图片&#xff0c;然后23行两条命令和在命令行中执行是一样的。

如何在Android 12 aosp系统源码中添加三指下滑截图功能

如何在Android 12 aosp系统源码中添加三指下滑截图功能 系统中截图api非常简单&#xff1a; private static ScreenshotHelper sScreenshotHelper;sScreenshotHelper new ScreenshotHelper(mContext);//调用 sScreenshotHelper.takeScreenshot(WindowManager.TAKE_SCREENSHO…