一、快捷键:
1.头文件申明的函数需要在相应的cpp文件中编写对应的函数定义实现
头文件定义了函数eat,鼠标移动到函数末尾;按下alt+enter会出现如下画面:
2.F4快捷切换头文件和源文件
二、自定义信号与槽函数需要重新定义两个类
自定义girlfriend和me两个类
girlfriend.h头文件(信号函数)
signals:
void hungry();
void annoyance();
void tired();
另外一个类处理信号:
me.h头文件(槽函数)
public slots:
void eat();
void drinkWater();
声明函数之后需要实现函数:
me.cpp
void Me::eat()
{
qDebug()<<"吃饭去";
}
void Me::drinkWater()
{
qDebug()<<"多喝热水";
}
函数实现之后两个对象之间没有连续,将两个对象创建到窗口中
先添加头文件
zuobiao.h
#include"me.h"
#include"girlfriend.h"
私有成员变量也需要添加进去
Me* m_me;
GirlFriend* m_girl;
源文件中初始化两个对象
m_me =new Me;
m_girl =new GirlFriend;
链接两个对象
connect(m_girl,&GirlFriend::hungry,m_me,&Me::eat);
connect(m_girl,&GirlFriend::hungry,this,&zuobiao::oneToManySlot);
connect(m_girl,&GirlFriend::annoyance,m_me,&Me::drinkWater);
connect(m_girl,&GirlFriend::tired,m_me,&Me::drinkWater);
指定信号的发射:
添加控件(可以为pushbutton等)
主窗口头文件zuobiao.h中添加槽函数
//添加hungry槽函数
void hungrySlot();
void oneToManySlot();
关联两个对象:
connect(m_girl,&GirlFriend::hungry,m_me,&Me::eat);
connect(m_girl,&GirlFriend::tired,m_me,&Me::drinkWater);
connect(m_girl,&GirlFriend::annoyance,m_me,&Me::drinkWater);
connect(m_girl,&GirlFriend::hungry,this,&zuobiao::oneToManySlot);
三、qt4的连接方式
connect(m_girl,SIGNAL(hungry()),m_me,SLOT(eat()));
connect(m_girl,SIGNAL(hungry(Qstring)),m_me,SLOT(eat(QString)));
四、信号函数重载之后需要指定指针指向函数
void (GirlFriend::*girl1)()=&GirlFriend::hungry;
void (GirlFriend::*girl2)(QString)=&GirlFriend::hungry;
void (Me::*me1)(QString)=&Me::eat;
五、完整代码
头文件
girlfriend.h
#ifndef GIRLFRIEND_H
#define GIRLFRIEND_H
#include <QObject>
class GirlFriend : public QObject
{
Q_OBJECT
public:
explicit GirlFriend(QObject *parent = nullptr);
signals:
void hungry();
void tired();
void annoyance();
void hungry(QString msg);
};
#endif // GIRLFRIEND_H
me.h
#ifndef ME_H
#define ME_H
#include <QObject>
class Me : public QObject
{
Q_OBJECT
public:
explicit Me(QObject *parent = nullptr);
public slots:
void eat();
void drinkWater();
void eat(QString msg);
};
#endif // ME_H
zuobiao.h
#ifndef ZUOBIAO_H
#define ZUOBIAO_H
#include <QMainWindow>
#include"me.h"
#include"girlfriend.h"
QT_BEGIN_NAMESPACE
namespace Ui { class zuobiao; }
QT_END_NAMESPACE
class zuobiao : public QMainWindow
{
Q_OBJECT
public:
zuobiao(QWidget *parent = nullptr);
~zuobiao();
QVariant dataplus(QVariant a, QVariant b);
//添加hungry槽函数
void hungrySlot();
void oneToManySlot();
void drinkWaterSlot();
private:
Ui::zuobiao *ui;
Me* m_me;
GirlFriend* m_girl;
};
//class Test : public QMainWindow{
// Q_OBJECT
// //自定义信号槽
//signals://关键字
// void testSignal();//必须返回void类型参数
// void textSignal(int a);//实参会被传递给槽函数
//};
#endif // ZUOBIAO_H
源文件
girlfriend.cpp
#include "girlfriend.h"
GirlFriend::GirlFriend(QObject *parent) : QObject(parent)
{
}
me.cpp
#include "me.h"
#include<QDebug>
Me::Me(QObject *parent) : QObject(parent)
{
}
void Me::eat()
{
qDebug()<<"吃饭去";
}
void Me::drinkWater()
{
qDebug()<<"多喝热水";
}
void Me::eat(QString msg)
{
qDebug()<<"我带你去吃:"<<msg;
}
zuobiao.cpp
#include "zuobiao.h"
#include "ui_zuobiao.h"
#include<QDebug>
#include<QPushButton>
zuobiao::zuobiao(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::zuobiao)
{
ui->setupUi(this);
m_me =new Me;
m_girl =new GirlFriend;
void (GirlFriend::*girl1)()=&GirlFriend::hungry;
void (GirlFriend::*girl2)(QString)=&GirlFriend::hungry;
void (Me::*me1)(QString)=&Me::eat;
connect(m_girl,girl2,m_me,me1);
connect(m_girl,&GirlFriend::tired,m_me,&Me::drinkWater);
connect(m_girl,&GirlFriend::annoyance,m_me,&Me::drinkWater);
connect(m_girl,girl1,this,&zuobiao::oneToManySlot);
connect(ui->hungryBtn,&QPushButton::clicked,this,&zuobiao::hungrySlot);
//qt4连接方式
connect(m_girl,SIGNAL(hungry()),m_me,SLOT(eat()));
connect(m_girl,SIGNAL(hungry(Qstring)),m_me,SLOT(eat(QString)));
connect(ui->closeBtn,&QPushButton::clicked,this,&QMainWindow::close);
// QVariant dataplus(QVariant a, QVariant b);
// int value = dataplus(10,20).toInt();
// QString str=dataplus("hello","world").toString();
// qDebug()<<"int value:"<<value;
// qDebug()<<"string value:"<<str;
}
zuobiao::~zuobiao()
{
delete ui;
}
void zuobiao::hungrySlot()
{
//发射信号
emit m_girl->hungry();//emit可有可无
emit m_girl->hungry("自助餐");
}
void zuobiao::oneToManySlot()
{
qDebug()<<"去吃海鲜";
}
void zuobiao::drinkWaterSlot()
{
qDebug()<<"duo he re shui";
}
//QVariant dataplus(QVariant a, QVariant b)
//{
// QVariant yet;
// if(a.type()==QVariant::Int&&b.type()==QVariant::Int)
// {
// yet=QVariant(a.toInt()+b.toInt());
// }
// else if(a.type()==QVariant::String&&b.type()==QVariant::String)
// {
// yet.setValue(a.toString()+b.toString());
// }
// return yet;
//}
main.cpp
#include "zuobiao.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
zuobiao w;
w.show();
return a.exec();
}
---------------------------------------------------------------------------------------------------------------------------------B站学习@爱编程的大丙->信号槽的使用的学习心得