0
点赞
收藏
分享

微信扫一扫

量化交易之QT篇 - 重载自定义的信号和槽(函数指针,QString转char*)


// TQZTeacher.h (TQZTeacher.cpp同上篇文章)

#ifndef TQZTEACHER_H
#define TQZTEACHER_H

#include <QObject>

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

signals:
// 自定义信号都写在这下面;
// 且信号无返回值, 只需要声明不需实现;
// 可以有参数, 可以重载;
void hungry();

void hungry(QString foodName);


public slots:
};

#endif // TQZTEACHER_H

// TQZStudent.h

#ifndef TQZSTUDENT_H
#define TQZSTUDENT_H

#include <QObject>

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

signals:

public slots:
void treat();

void treat(QString foodName);
};

#endif // TQZSTUDENT_H

// TQZTeacher.cpp

#include "TQZStudent.h"
#include <QDebug>


TQZStudent::TQZStudent(QObject *parent) : QObject(parent)
{

}

void TQZStudent::treat() {
qDebug() << __func__;
}

void TQZStudent::treat(QString foodName) {
qDebug() << __func__ << foodName.toUtf8().data();
}

// Widget.cpp (Widget.h 同上篇文章)

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

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

this->teacher = new TQZTeacher(this);
this->student = new TQZStudent(this);

// connect(this->teacher, &TQZTeacher::hungry, this->student, &TQZStudent::treat);
// this->classIsOver();

void(TQZStudent:: *studentFunction)(QString) = &TQZStudent::treat;
void(TQZTeacher:: *teacherFunction)(QString) = &TQZTeacher::hungry;
connect(this->teacher, teacherFunction, this->student, studentFunction);

this->classIsOver();
}

void Widget::classIsOver() {
// 触发 this->teacher 的 hungry 方法;
// emit this->teacher->hungry();
emit this->teacher->hungry(QString("dongdamen shaokao."));

}

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

举报

相关推荐

0 条评论