C++ 自定义QPushButton有参信号
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include<QWidget>
#include"teacher.h"
#include"student.h"
#include <QMainWindow>
namespace Ui {
class MyWindow;
}
class MyWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MyWindow(QWidget *parent = nullptr);
~MyWindow();
private:
Ui::MyWindow *ui;
Teacher *zt;
Student *st;
void classIsOver();
};
#endif // MYWINDOW_H
#ifndef STUDENT_H
#define STUDENT_H
#include <QObject>
class Student : public QObject
{
Q_OBJECT
public:
explicit Student(QObject *parent = nullptr);
signals:
public slots:
//早期Qt版本,必须写到public slots,高级版本可以下到public或者全局下
//返回值void,声明,也需要实现
//可以有参数,可以发生重载
void treat();
void treat(QString foodName);
};
#endif // STUDENT_H
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
class Teacher : public QObject
{
Q_OBJECT
public:
explicit Teacher(QObject *parent = nullptr);
signals:
public slots:
//自定义信号写到signals下
//返回值是void,只需要声明,不需要实现
//可以有参数,可以重载
void hungry();
void hungry(QString foodName);
};
#endif // TEACHER_H
#include "mywindow.h"
#include "ui_mywindow.h"
#include "QPushButton"
#include "QLineEdit"
#include"mywindow.h"
#include "student.h"
#include "teacher.h"
MyWindow::MyWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyWindow)
{
ui->setupUi(this);
//创建一个老师对象
this->zt=new Teacher(this);
//创建一个学生对象
this->st=new Student(this);
//老师饿了,学生请客的链接
//connect(zt,&Teacher::hungry,st,&Student::treat);
//classIsOver();
void (Teacher:: * teacherSignal)(QString)=&Teacher::hungry;
void (Student:: * studentSlot)(QString)=&Student::treat;
connect(zt,teacherSignal,st,studentSlot);
classIsOver();
}
void MyWindow::classIsOver()
{
//emit zt->hungry();
emit zt->hungry("宫保鸡丁");
}
MyWindow::~MyWindow()
{
delete ui;
}
#include "student.h"
#include "QDebug"
Student::Student(QObject *parent) : QObject(parent)
{
}
void Student::treat()
{
qDebug()<<"请老师吃饭";
}
void Student::treat(QString foodName)
{
qDebug()<<"请老师吃饭"<<foodName;
}
#include "teacher.h"
#include "QDebug"
Teacher::Teacher(QObject *parent) : QObject(parent)
{
}
void Teacher::hungry()
{
qDebug()<<"老师饿了";
}
void Teacher::hungry(QString foodName)
{
qDebug()<<"老师饿了"<<foodName;
}