QT入门1

student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

class Student : public QObject
{
    Q_OBJECT
public:
    explicit Student(QObject *parent = nullptr);

    void  treat();

    void  treat(QString foodName);

signals:


};

#endif // STUDENT_H

teacher.h

#ifndef TEACHER_H
#define TEACHER_H

#include <QObject>

class Teacher : public QObject
{
    Q_OBJECT
public:
    explicit Teacher(QObject *parent = nullptr);

signals://自定义信号
//返回值void
//只需要声明,不需要实现
//可以重载
    void hungry();

    void hungry(QString foodName);


};

#endif // TEACHER_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include"teacher.h"
#include"student.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;

    Teacher * a;
    Student * b;

    void ClassIsOver();
};
#endif // WIDGET_H

student.cpp

#include "student.h"
#include<QDebug>
Student::Student(QObject *parent) : QObject(parent)
{

}

void Student::treat()
{
    qDebug() << "吃饭";
}

void Student::treat(QString foodName)
{
    qDebug() << "吃xxxxx:" << foodName.toUtf8().data();
}

teacher.cpp

#include "teacher.h"

Teacher::Teacher(QObject *parent) : QObject(parent)
{

}

main.cpp

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建Teacher对象
    this->a = new Teacher(this);
    //Student
    this->b = new Student(this);

    //connect(a,&Teacher::hungry,b,&Student::treat);

    void(Teacher::*teacherSingal)(QString) = &Teacher::hungry;
    void(Student::*studentSingal)(QString) = &Student::treat;
    connect(a,teacherSingal,b,studentSingal);
    // 调用
    ClassIsOver();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::ClassIsOver()
{
    //触发信号
    //emit a->hungry();
    emit a->hungry("嘉然");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值