connect函数
connect(sender, signal, receiver, slot);
槽函数示例
void student:: treat(QString foodname)
void student:: treat(int index)
由上可见,有两个名字相同,但形参不同的槽函数。
可以通过函数指针的方式 ,用指针指向具体的函数。如下所示
//连接
//connect(zt,&teacher::hungery,st,&student::treat);
void (teacher:: *teachersignals)(QString) = &teacher::hungery;
void (student:: *studentslots)(QString) = &student::treat;
connect(zt,teachersignals,st,studentslots);
示例
void student::treat(QString foodname)
{
qDebug()<<"请老师吃"<<foodname;
}
class teacher : public QObject
{
Q_OBJECT
public:
explicit teacher(QObject *parent = nullptr);
signals:
//自定义信号
//返回值是void,只需要声明,不需要实现
//可以有参数,可以重载
void hungery();
void hungery(QString foodname);
};
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//创建对象
this->zt = new teacher(this);
this->st = new student(this);
//连接
//connect(zt,&teacher::hungery,st,&student::treat);
void (teacher:: *teachersignals)(QString) = &teacher::hungery;
void (student:: *studentslots)(QString) = &student::treat;
connect(zt,teachersignals,st,studentslots);
classover();
}
void MainWindow::classover()
{
//emit 触发信号
// emit zt->hungery();
emit zt->hungery("宫保鸡丁");
}
MainWindow::~MainWindow()
{
delete ui;
}
现象