首页 > 编程语言 >day03(C++)信号槽

day03(C++)信号槽

时间:2024-10-15 12:46:35浏览次数:3  
标签:include 信号 day03 void C++ Dialog btn 函数

目录

1. 概念

2. 函数原型

3. 连接方式

3.1 自带信号 → 自带槽

3.2 自带信号 → 自定义槽

3.3 自定义信号

4. 信号槽传参

5. 对应关系

5.1 一对多

5.2 多对一


信号槽

1. 概念

之前的程序界面只能看,不能交互,信号槽可以让界面进行人机交互。

信号槽是Qt在C++基础上新增的特性,类似于其他编程中的回调机制,其目的是实现对象之间的通信。

使用信号槽需要具备两个先决条件:

  • 通信的对象必须继承自QObject

QObject是Qt所有对象的基类,内部规定了Qt最基础的对象特性。

  • 类中要包含Q_OBJECT宏

2. 函数原型

信号槽的建立是通过connect函数实现的。

// 信号槽连接函数
// 参数1:发射者,因发起的对象n.
// 参数2:信号函数,因的动作v.,需要使用SIGNAL()包裹函数名称
// 参数3:接收者,果执行的对象n.
// 参数4:槽函数,果的动作v.,需要使用SLOT()包裹函数名称
QObject::​connect(const QObject * sender, 
				    const char * signal, 
				    const QObject * receiver, 
				    const char * slot)                [static]

信号函数一定来自于发射者,槽函数一定来自于接收者。信号槽的连接必须在发射者对象和接收者对象创建完成后进行,如果成功连接后,发射者或接收者对象销毁了,连接断开。

信号槽的连接也可以程序员手动断开,只需要把connect函数改为disconnect即可,参数保持不变。

3. 连接方式

为了理解,按照难易程度分为三种连接方式:

  • 自带信号 → 自带槽
  • 自带信号 → 自定义槽
  • 自定义信号 → 槽

3.1 自带信号 → 自带槽

这种连接方式是最简单的连接方式,因为信号函数和槽函数都是Qt内置的,程序员只需要找到连接的四个参数,直接连接即可。

【例子】点击按钮,关闭窗口。

分析:

参数1:发射者——按钮对象

参数2:信号函数——点击

void QAbstractButton::​clicked()                    [signals]

参数3:接收者——窗口对象

参数4:槽函数——关闭

bool QWidget::​close()                            [slot]

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton *btn;
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    resize(400,400);
    btn = new QPushButton("关闭",this);
    btn->move(100,100);
    //  参数1:发射者——按钮对象
    //	参数2:信号函数——点击
    //	参数3:接收者——窗口对象
    //	参数4:槽函数——关闭
    connect(btn,SIGNAL(clicked()),this,SLOT(close()));
}

Dialog::~Dialog()
{
    delete btn;
}

3.2 自带信号 → 自定义槽

这种连接方式是使用的最多的一种连接方式,因为在实际开发中,Qt无法预设所有的槽函数情况,对于复杂的操作执行逻辑,需要程序员手动编写槽函数。

槽函数是一种特殊的成员函数。

【例子】点击按钮,窗口向右下角移动10个像素,并且后台输出当前窗口的位置。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton *btn;

    // 槽函数的声明
private slots: // 最小权限原则
   void mySlot(); // 自定义槽函数
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    resize(400,400);
    btn = new QPushButton("关闭",this);
    btn->move(100,100);
    // 连接信号槽
    connect(btn,SIGNAL(clicked()),
            this,SLOT(mySlot()));
}

void Dialog::mySlot()
{
    // 先获得当前坐标
    int x = this->x();
    int y = this->y();
    // 移动
    move(x+10,y+10);
    // 输出当前窗口位置
    qDebug() << this->x() << this->y();
}

Dialog::~Dialog()
{
    delete btn;
}

3.3 自定义信号

自定义信号通常用于解决一些对象之间“远距离”通信问题,本节属于强行使用,因此并不是问题最优解,只是为了展示自定义信号的使用方式。

【例子】点击按钮,按钮上显示点击的次数。

先忽略自定义信号,展示正常解法:

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton* btn;
    int count;

private slots:
    void btnClickedSlot(); // 点击按钮的槽函数
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent),count(0) // 构造初始化列表
{
    resize(400,400);
    btn = new QPushButton("0",this);
    btn->move(200,200);

    connect(btn,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot()));
}

void Dialog::btnClickedSlot()
{
    count++;
    // int → QString
    QString text = QString::number(count);
    // 设置到按钮上显示
    btn->setText(text);
}

Dialog::~Dialog()
{
    delete btn;
}

基于上面的解法,强行增加自定义信号发射环节:

信号函数是一种特殊的函数,只有声明没有定义,声明后可以直接配合emit关键字发射。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton* btn;
    int count;

signals:
    void countSignal(); // 自定义信号

private slots:
    void btnClickedSlot(); // 点击按钮的槽函数
    void countSlot(); // 与自定义信号连接的槽函数
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent),count(0) // 构造初始化列表
{
    resize(400,400);
    btn = new QPushButton("0",this);
    btn->move(200,200);

    connect(btn,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot()));
    connect(this,SIGNAL(countSignal()),
            this,SLOT(countSlot()));
}

void Dialog::btnClickedSlot()
{
    count++;
    // 发射自定义信号
    emit countSignal();
}

/**
 * @brief Dialog::countSlot
 * 中转之后更新按钮显示的槽函数
 */
void Dialog::countSlot()
{
    QString text = QString::number(count);
    btn->setText(text);
}

Dialog::~Dialog()
{
    delete btn;
}

4. 信号槽传参

信号槽可以进行参数传递,信号函数携带参数发射,槽函数可以收到此参数。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton* btn;
    int count;

signals:
    void countSignal(int); // 自定义信号

private slots:
    void btnClickedSlot(); // 点击按钮的槽函数
    void countSlot(int); // 与自定义信号连接的槽函数
};

#endif // DIALOG_H


dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent),count(0) // 构造初始化列表
{
    resize(400,400);
    btn = new QPushButton("0",this);
    btn->move(200,200);

    connect(btn,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot()));
    connect(this,SIGNAL(countSignal(int)),
            this,SLOT(countSlot(int)));
}

void Dialog::btnClickedSlot()
{
    count++;
    // 发射自定义信号(携带参数)
    emit countSignal(count);
}

/**
 * @brief Dialog::countSlot
 * count参数是信号函数发来的,不是成员变量
 * 中转之后更新按钮显示的槽函数
 */
void Dialog::countSlot(int count)
{
    QString text = QString::number(count);
    btn->setText(text);
}

Dialog::~Dialog()
{
    delete btn;
}

需要注意的是:

  • 理论上可以传递任意多个参数
  • 信号函数的参数个数必须大于等于槽函数的参数个数
  • 参数类型需要匹配

5. 对应关系

同一个信号可以连接到多个槽(一对多),多个信号也可以连接到同一个槽(多对一)。

5.1 一对多

槽函数也是成员函数,因此在一对多的连接关系中,把连接的多个槽函数可以看做是普通的成员函数,合并为一个槽函数。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton* btn1;
    QPushButton* btn2;

private slots:
    void btnClickedSlot1();
    void btnClickedSlot2();
    void btnClickedSlot3(); // 3 = 1 + 2
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    resize(400,400);
    btn1 = new QPushButton("一对多", this);
    btn1->move(200,100);
    btn2 = new QPushButton("一对一", this);
    btn2->move(100,200);

    // 一对多
    connect(btn1,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot1()));
    connect(btn1,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot2()));
    // 一对一
    connect(btn2,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot3()));
}

void Dialog::btnClickedSlot1()
{
    qDebug() << "A";
}

void Dialog::btnClickedSlot2()
{
    qDebug() << "B";
}

void Dialog::btnClickedSlot3()
{
    // 直接把Slot1和Slot2两个函数当做普通的成员函数,
    // 使用this指针调用
    btnClickedSlot1();
    btnClickedSlot2();
}

Dialog::~Dialog()
{
    delete btn1;
}

5.2 多对一

多对一的连接方式下,槽函数无法区分信号来源,可以在槽函数中调用sender函数获取发射者对象,从而判断信号来源。

// 在槽函数中调用,返回发射者对象
QObject * QObject::​sender() const

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QPushButton* btn1;
    QPushButton* btn2;

private slots:
    void btnsClickedSlot();
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    resize(400,400);
    btn1 = new QPushButton("A",this);
    btn2 = new QPushButton("B",this);
    btn1->move(150,100);
    btn2->move(150,200);

    // 多对一
    connect(btn1,SIGNAL(clicked()),
            this,SLOT(btnsClickedSlot()));
    connect(btn2,SIGNAL(clicked()),
            this,SLOT(btnsClickedSlot()));
}

void Dialog::btnsClickedSlot()
{
    if(btn1 == sender())
    {
        qDebug() << "点击了按钮1";
    }else if(btn2 == sender())
    {
        qDebug() << "点击了按钮2";
    }
    qDebug() << "槽函数触发!";
}

Dialog::~Dialog()
{
    delete btn1;
    delete btn2;
}

标签:include,信号,day03,void,C++,Dialog,btn,函数
From: https://blog.csdn.net/QR70892/article/details/142930182

相关文章

  • RBE104TC C/C++ Programming Language
    RBE104TCC/C++ProgrammingLanguageAssignment1ContributiontotheOverallMarks30%IssueDateSubmissionDeadline13thOctober2024AssignmentOverview:ThisassignmentisgearedtowardsassessingfundamentalcodingconceptsinC/C++andinitiatingthep......
  • c++基础语法知识
    基础当VS中一个项目下有两个及以上的源文件时,编译会产生错误:main已经在test.obj中定义;找到一个或多个多重定义的符号。**解决办法:**将不需要编译的源文件排除:右键“属性”,将“从生成中排除”选择“是”,保存后再运行需要运行的源文件就可以成功,且被排除的文件右下角有红标......
  • C++面试速通宝典——27
    504.孤儿进程和僵尸进程是什么?怎么处理?孤儿进程:当一个父进程结束,而他的一个或多个子进程还在运行时,那些子进程将成为孤儿进程。孤儿进程会被init进程(进程ID为1)自动领养,init进程会负责调用wait()来收集他们的退出状态。僵尸进程:当一个子进程结束,在其父进程没有调用wait()......
  • C++试题带答案
    一、选择填空题1.有如下定义structperson{char name[9];intage;};person  pr[10]={"Johu",17,"Paul",19,"Mary",18,"Adam",16};根据上述定义,能输出字母M的语句是     A)cout<<pr[3].mane;          B)cout<<pr[3].name[1];C......
  • 【C++指南】C++中的浅拷贝与深拷贝:深入剖析
              ......
  • 实验1 现代C++编程初体验
    实验任务1代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest......
  • 【子项目:命令系统(Command System)】C++自制命令系统( 开发ing | 踩坑记录 )
    项目背景在某一项目中,遇到了需要自制命令系统的需求,而这个模块的复用性很高,因此单独拉出来做一个子项目更新日志[2024.10.15-10:00]增项目进度----[2024.10.1510:00]----首先实现最基础的输入输出功能,用std::getline读入行再分割成字符串数组-main.cpp#include......
  • C++ 面向对象、特征、重载和重写、构造和析构、向上和向下转型、深浅拷贝。
    什么是面向对象(Object-OrientedProgramming,OOP)1.面向对象是一种编程范式,它通过将软件系统的设计和开发分解为“对象”(Object)的方式来实现更好地组织代码。面向对象的核心思想是将程序的结构分为对象,这些对象包含数据和操作这些数据的函数(即方法)。每个对象是类的实例,而类定......
  • GESP2024年6月认证C++四级( 第三部分编程题(1))
    参考程序代码:#include<bits/stdc++.h>usingnamespacestd;constintN=55;intw[N][N];intn,m;boolcheck(intxa,intya,intxb,intyb){ inta[2]={0,0}; for(inti=xa;i<=xb;i++) { for(intj=ya;j<=yb;j++) { a[w[1][j]]++; } } returna[0......
  • C++模板初阶,只需稍微学习;直接起飞;泛型编程
    ......