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

news2024/9/20 1:27:19

一 信号与槽函数

#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

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

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

相关文章

【实施文档】软件项目实施方案(Doc原件2024实际项目)

软件实施方案 二、 项目介绍 三、 项目实施 四、 项目实施计划 五、 人员培训 六、 项目验收 七、 售后服务 八、 项目保障措施软件开发管理全套资料包清单&#xff1a; 工作安排任务书&#xff0c;可行性分析报告&#xff0c;立项申请审批表&#xff0c;产品需求规格说明书&am…

【文献阅读】Unsupervised Machine Learning for Bot Detection on Twitter

Abstract 引入新特征&#xff0c;并降低所提模型的复杂性&#xff0c;从而提高基于聚类算法的机器人识别准确性。 最小化数据集维度和选择重要特征来实现的。 实验证明该方法的特征可以与四种不同的聚类技术&#xff08;agglomerating、k-medoids、DBSCAN 和 K-means&#x…

Android Graphics 显示系统 - VirtualDisplay mirrorDisplay 简单示例

“ Life is like a box of chocolates, you never konw what youre going to get。最近我也得到了一块巧克力&#xff0c;迫不及待地想尝一下甜的惊喜 。” 前言 上一篇文章&#xff0c;我们分享了一个VirtualDisplay的简单实例&#xff0c;主要是为了引入创建虚拟屏幕都使用了…

C# 如何检查两个给定的线段是否相交(How to check if two given line segments intersect)

给定两条线段(p1, q1)和(p2, q2)&#xff0c;判断给定的线段是否相交。 在讨论解决方案之前&#xff0c;让我们先定义方向的概念。平面中有序点三元组的方向可以是 –逆时针 –顺时针 –共线 下图显示了&#xff08;a&#xff0c;b&#xff0c;c&#xff09; 的不同可能方…

多进程批量下载era5再分析数据

1.配置key https://cds.climate.copernicus.eu/api-how-to 获取key 修改配置文件&#xff0c;把url和key复制进行 vim $HOME/.cdsapirc2.下载 根据要求修改年和月份等变量 import cdsapi import calendar import concurrent.futures import osdef download_month_data(year,…

KEIL编译生成.bin文件的简单方法

fromelf --bin -o "$LL.bin" "#L" 如图 如果不行请尝试其他方法

大模型算法入行转行?我建议你这样做!

最近私信问我关于入行、转行方面的问题比较多&#xff0c;就专门写一篇讲讲我的理解。 首先说明一下个人的背景和现状&#xff0c;我本人是本科学历&#xff0c;有互联网大厂搜推方向经验&#xff0c;后来跳到中厂继续做推荐&#xff0c;去年开始做大模型。现在是个小组长&…

c中 int 和 unsigned int

c语言中&#xff0c;char、short、int、int64以及unsigned char、unsigned short、unsigned int、unsigned int64等等类型都可以表示整数。但是他们表示整数的位数不同&#xff0c;比如&#xff1a;char/unisigned char表示8位整数&#xff1b; short/unsigned short表示16位整…

桂林自闭症寄宿学校:用关爱点亮未来

在桂林这座风景如画的城市中&#xff0c;隐藏着一所特别的学校&#xff0c;它以无尽的关爱与专业&#xff0c;为自闭症儿童撑起了一片希望的天空&#xff0c;这就是星贝育园自闭症儿童寄宿制学校。在这里&#xff0c;每一个孩子都是独一无二的&#xff0c;他们被温柔以待&#…

仪器计量校准的设备保养方法有哪些?

仪器校准、检定&#xff0c;是对设备和仪器进行校正和校验。与规范所再现的相应值相关联的一组检测&#xff0c;以规定精确测量仪器或检测系统所指示的值&#xff0c;及产品测量仪器和对照化学物质所隐含的值&#xff0c;是否符合所要求的标准。 仪器校准可能包括以下过程&…

postgresql|数据库|pg_repack和idle_in_transaction_session_timeout参数的关系

一、问题描述 在使用pg_repack这个工具做数据库的表膨胀清理过程中&#xff0c;经常会遇到类似这样的警告&#xff1a; 这里的警告表明在膨胀治理的时候&#xff0c;此表遇到了事务阻塞&#xff0c;而此时我们有三种选择&#xff0c;第一个选择是等待该事务结束&#xff0c;第…

在Excel里制作简单游戏界面

生成随机激活码 找工具箱 插入按钮 建宏 方法一&#xff1a;新建按钮的时候创建宏 方法二&#xff1a;右键->指定宏 VBA VBA代码界面 调整字体 VBA代码 Public str As String 存储激活码显示的字符 Public st As String 中间变量&#xff0c;用来替代随机数 Public ot…

[实践应用] 深度学习之激活函数

文章总览&#xff1a;YuanDaiMa2048博客文章总览 深度学习之激活函数 激活函数基本概念分类常见的激活函数2. Tanh/双曲正切激活函数3. ReLU激活函数4. Softmax激活函数 PyTorch中如何使用1. 线性激活函数2. 非线性激活函数SigmoidTanhReLULeaky ReLUParametric ReLU (PReLU) 使…

ThinkPHP Email功能如何配置才能发送邮件?

ThinkPHP Email发送流程&#xff1f;使用ThinkPHP发Email方法&#xff1f; ThinkPHP作为一款流行的PHP框架&#xff0c;提供了强大的Email功能&#xff0c;使得开发者能够轻松实现邮件发送。AokSend将详细介绍如何配置ThinkPHP Email功能&#xff0c;以确保邮件能够顺利发送。…

基于多种智能优化算法优化BP神经网络的数据时序预测

基于多种智能优化算法优化BP神经网络进行数据时序预测的研究&#xff0c;旨在通过引入多种优化算法来提高传统BP神经网络&#xff08;Backpropagation Neural Network&#xff09;的预测精度与泛化能力。 代码原理及流程 1. BP神经网络简介 BP神经网络是一种常见的前馈神经网…

别找了!包含gpt在内的国内可以使用的Ai网站都在这了【最新可用】

在当今人工智能迅速发展的时代&#xff0c;智能创作与对话平台为用户提供了多样化的功能支持。以下是一些国内代表性的GPT平台&#xff0c;涵盖了从个人到企业的广泛需求&#xff0c;您可以根据自己的需求灵活选择。我们还为您整理了这些平台的链接&#xff0c;方便直接体验。&…

谷歌对抗司法部:为什么谷歌的“数百个竞争对手”说法站不住脚

随着谷歌反垄断陪审团审判的进行&#xff0c;谷歌声称美国司法部对广告技术市场的看法狭隘&#xff0c;并且广告商和出版商有很多替代选择。然而&#xff0c;证据并不支持这一说法。 谷歌误导性地声称有“数百个竞争对手。” 虽然存在许多广告技术提供商&#xff0c;但谷歌在…

【Petri网导论学习笔记】Petri网导论入门学习(二)

Petri 网导论学习笔记&#xff08;二&#xff09; 如需学习转载请注明原作者并附本帖链接&#xff01;&#xff01;&#xff01; 如需学习转载请注明原作者并附本帖链接&#xff01;&#xff01;&#xff01; 如需学习转载请注明原作者并附本帖链接&#xff01;&#xff01;&am…

如何进行DAP-seq的数据挖掘,筛选验证位点

从样本准备到寄送公司&#xff0c;每一天都在“祈祷”有个心仪的分析结果&#xff0c;终于在这天随着邮件提示音的响起&#xff0c;收到了分析结果...... 分析前工作 爱基在进行数据分析之前&#xff0c;会有两次质控报告反馈给老师们。第一个&#xff0c;基因组DNA的提取质控…

线上找工作求职招聘小程序源码系统 带完整的安装代码包以及搭建部署教程

系统概述 在当今这个数字化时代&#xff0c;互联网已经渗透到我们生活的方方面面&#xff0c;其中就业市场也不例外。随着移动互联网的普及&#xff0c;线上找工作已成为许多求职者的首选方式。为了满足这一市场需求&#xff0c;我们精心打造了一款“线上找工作求职招聘小程序…