=============== mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"teacher.h"
#include"student.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void classIsOver();
Teacher *ls;
Student *fz;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
======================= mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include"teacher.h"
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *mybtn = new QPushButton(0);
mybtn->setParent(this);
mybtn->QPushButton::setText("我的按钮");
mybtn->resize(200,40);
mybtn->move(100,50);
this->ls = new Teacher(this);
this->fz = new Student(this);
void(Teacher::*teacherSignal)(void)= &Teacher::hungry2;
void(Student::*studentSlot)(void) = &Student::treat;
// void(Student::*studentSlot)() = &Student::treat;
connect(ls,teacherSignal,fz,studentSlot);
// void(Teacher::*teacherSlot)() = &Teacher::hungry2;
connect(mybtn,&QPushButton::clicked,ls,teacherSignal);// clicked 这个信号的参数为 bool 类型,所以hungry2()作为槽函数也不能有参数(或者只能也是bool型参数)
//信号可以连接槽函数,也可以连接信号函数
}
//void MainWindow::classIsOver(){
// ls->hungry2("宫保鸡丁");
//}
MainWindow::~MainWindow()
{
delete ui;
}
=============== student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <QObject>
#include<QDebug>
#pragma once
class Student : public QObject
{
Q_OBJECT
public:
explicit Student(QObject *parent = nullptr);
void treat();
void treat(QString food);
signals:
};
#endif // STUDENT_H
====================== teacher.h
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
#include<QDebug>
#pragma once
class Teacher : public QObject
{
Q_OBJECT
public:
explicit Teacher(QObject *parent = nullptr);
// void hungry2(QString food);
signals:
// void hungry2();
// inline void hungry2(QString food);
// inline void hungry2();
void hungry2(QString food);
void hungry2();
};
#endif // TEACHER_H
===================== students.cpp
#include "student.h"
Student::Student(QObject *parent) : QObject(parent)
{
}
void Student::treat(QString food){
qDebug()<<"请老师吃:"<<food<<endl;
}
void Student::treat(){
qDebug()<<"请老师吃饭"<<endl;
}
================ teacher.cpp
#include "teacher.h"
Teacher::Teacher(QObject *parent) : QObject(parent)
{
}
void Teacher::hungry2(QString food){
qDebug()<<"老师饿了,要吃:"<<food;
}
void Teacher::hungry2(){
qDebug()<<"老师饿了,要吃:";
}
想要实现信号连接信号的功能,发现一直报错
后面看了这篇文章在 hungry2() 和 hungry2(QString food) 加上 inline 就好了!
【LNK1169】找到一个或多个多重定义的符号_lyorz的博客-优快云博客_lnk1169找到一个或多个多重定义的符号
还不是很明白,对内联函数不清楚,还是要加强学习啊