美文网首页
2020-01-09 qt第二天

2020-01-09 qt第二天

作者: 培根好吃 | 来源:发表于2020-01-09 12:25 被阅读0次
    // widget.cpp
    #include "widget.h"
    #include <QPushButton>
    
    //需求:创建两个类 Teacher类 Student类
    //下课后 老师发出一个信号 饿了
    //学生响应信号,处理信号的槽函数,请老师吃饭
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        tc = new Teacher(this);
        st = new Student(this);
        //connect(tc,&Teacher::hungry,st,&Student::treat);
        void(Teacher::*teacherSignal)(QString) = &Teacher::hungry;
        void(Student::*studentSlot)(QString) = &Student::treat;
    
        // 无参数触发信号
        connect(tc,teacherSignal,st,studentSlot);
        classIsOver();
    
        // 有参数触发信号
        //QPushButton* btn = new QPushButton;
        //btn->setParent(this);
        //btn->setText("下课");
        //connect(btn,&QPushButton::clicked,tc,teacherSignal);
        //connect(tc,teacherSignal,st,studentSlot);
    }
    
    void Widget::classIsOver(){
        //emit tc->hungry();
        emit tc->hungry("吃鸡");
    
    }
    
    Widget::~Widget()
    {
    
    }
    //teacher.cpp
    //student.cpp
    #include "student.h"
    #include <QDebug>
    
    Student::Student(QObject *parent) : QObject(parent)
    {
    
    }
    void Student::treat(){
        qDebug()<<"请老师吃饭";
    }
    void Student::treat(QString foodName){
        qDebug()<<"请老师吃饭"<<foodName.toUtf8().data();
    }
    
    //widget
    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include "teacher.h"
    #include "student.h"
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    
        Teacher *tc;
        Student *st;
        //QString foodName;
        void classIsOver();
    
    };
    
    #endif // WIDGET_H
    
    
    #ifndef TEACHER_H
    #define TEACHER_H
    
    #include <QObject>
    
    class Teacher : public QObject
    {
        Q_OBJECT
    public:
        explicit Teacher(QObject *parent = 0);
    
    signals:
        void hungry();
        void hungry(QString foodName);
    
    public slots:
    };
    
    #endif // TEACHER_H
    
    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include <QObject>
    
    class Student : public QObject
    {
        Q_OBJECT
    public:
        explicit Student(QObject *parent = 0);
    
    signals:
    
    public slots:
        void treat();
        void treat(QString foodName);
    };
    
    #endif // STUDENT_H
    
    
    

    相关文章

      网友评论

          本文标题:2020-01-09 qt第二天

          本文链接:https://www.haomeiwen.com/subject/sgdyactx.html