QT学习笔记(四)信号槽与简单的自定义信号

1. 坐标系

左上角为零点,x向右为正方向,y向下为正方形
在这里插入图片描述

2. 信号

完成连接connect的过程包括以下内容:

  • 信号的发送
  • 信号发送的具体内容
  • 信号的接受
  • 信号的处理(称为槽函数)

3. 信号槽

信号槽的优点: 松散耦合
信号的发送方和接受方本身没有关联(发送端的信号不一定触发接收端处理,接收端也不一定要接收信号后才完成操作),只是通过连接connect将两端耦合在一起。

4. 代码实现点击按钮关掉窗口

#include "mywidget.h"
#include "QPushButton"

mywidget::mywidget(QWidget *parent)
    : QWidget(parent)
{
    //创建按钮
    QPushButton *btn2=new QPushButton("button2",this);//button2为显示文本,this指依赖当前窗口
    
    //改变按钮位置
    btn2->move(100,100);

    //重置按钮大小
    btn2->resize(100,50);
    
    //设置窗口大小
    resize(300,200);//初始窗口大小,用户仍可以拖动

    //建立连接(发送方,信号内容,接受方,信号槽)
    connect(btn2,&QPushButton::clicked,this,&mywidget::close);
}

mywidget::~mywidget()
{
}

效果:点击button2实现关闭
在这里插入图片描述

5. 自定义信号槽

要实现一个效果:

//Mother类
//Daughter类
//下课-》我饿了-》我妈请吃饭
  1. 生成Daughter类和Mother类
    1.1 Daughter类
    右键工程->添加新文件->C++ Class->choose
    在这里插入图片描述
    创建Daughter:
    在这里插入图片描述
    1.2. Mother类
    同样的方法创建Mother类

  2. signals
    2.1 可以看到.h文件下,会有个自带的signals,自定义的信号就写在这里,写法如下:
    ① 返回void 信号
    ② 只需要声明,不需要实现
    ③ 可以有参数,因此可以重载
    2.2 例子:在daughter类里声明一个hungry信号

#ifndef DAUGHTER_H
#define DAUGHTER_H

#include <QObject>

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

signals:
    //自定义信号写在这里
    //返回void
    //信号只需要声明,不需要实现
    //可以有参数,因此可以重载

    //声明饿了的信号
    void hungry();

};
#endif // DAUGHTER_H
  1. slot
    3.1 槽函数,用来处理信号,不能写在signals部分,可以卸载public或者public slots(老版本),说明:
    ① 返回void,需要声明和定义
    ② 可以有参数,可以重载
    3.2 例子:在mother.h里写一个声明,在mother.cpp里写一个定义
//mother.h
#ifndef MOTHER_H
#define MOTHER_H

#include <QObject>

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

    //槽函数写在public下(老版本在public slot下)
    //返回void,需要声明和定义
    //可以有参数,可以重载
    void treat();
signals:
};
#endif // MOTHER_H
// mother.cpp
#include "mother.h"
#include <QDebug>
Mother::Mother(QObject *parent)
    : QObject{parent}
{
}
void Mother::treat()
{
    qDebug()<<"请女儿吃饭";
}
  1. 建立连接
    首先在widget.h声明两个指针
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    // 声明两个默认的指针
    Mother * m;
    Daughter *d;
};
#endif // WIDGET_H

再在widget.cpp创建对象和连接

#include "widget.h"
#include "ui_widget.h"


//Mother类
//Daughter类
//下课-》我饿了-》我妈请吃饭


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化我的指针,创建对象
    this->m=new Mother(this);//放到对象树里就不用管理释放了
    this->d=new Daughter(this);

    //建立连接(发送方,信号内容,接受方,信号槽)
    connect(d,&Daughter::hungry,m,&Mother::treat);
}

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

此时运行并不会有任何输出,因为并未触发女儿饿了的信号,增加这部分,在widget.h中增加classisover的声明

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include"mother.h"
#include"daughter.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    // 声明两个默认的指针
    Mother * m;
    Daughter *d;

    void classisover();

};
#endif // WIDGET_H

在widget.cpp中实现触发功能,其中emit是触发的意思

void Widget::classisover()
{
    emit d->hungry();
}

并在主函数中调用classisover,注意,调用classisover必须在建立连接以后

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化我的指针,创建对象
    this->m=new Mother(this);//放到对象树里就不用管理释放了
    this->d=new Daughter(this);

    //建立连接(发送方,信号内容,接受方,信号槽)
    connect(d,&Daughter::hungry,m,&Mother::treat);

    classisover();

}

效果为输出“请女儿吃饭”。
另外,可以加上前面的button部分,实现点击按钮触发classisover,整体的widget.cpp代码为:

#include "widget.h"
#include "ui_widget.h"
#include "QPushButton"


//Mother类
//Daughter类
//下课-》我饿了-》我妈请吃饭


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建按钮
    QPushButton *btn2=new QPushButton("button2",this);//button2为显示文本,this指依赖当前窗口

    //改变按钮位置
    btn2->move(100,100);

    //重置按钮大小
    btn2->resize(100,50);

    //设置窗口大小
    resize(300,200);//初始窗口大小,用户仍可以拖动


    //初始化我的指针,创建对象
    this->m=new Mother(this);//放到对象树里就不用管理释放了
    this->d=new Daughter(this);


    //建立连接(发送方,信号内容,接受方,信号槽)
    connect(d,&Daughter::hungry,m,&Mother::treat);


    //建立连接(发送方,信号内容,接受方,信号槽)
    connect(btn2,&QPushButton::clicked,this,&Widget::classisover);

}

Widget::~Widget()
{
    delete ui;
}
void Widget::classisover()
{
    emit d->hungry();
}

点击按钮输出请女儿吃饭
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值