网站哪类业务建设投入会带来间接收益,云开发壁纸小程序,wordpress防伪码,网站如何让百度收录第二天
2.1自定义信号和槽
新建一个Qtclass 自定义信号#xff1a;返回值是void #xff0c;只需要声明#xff0c;不需要实现#xff0c;可以有参数#xff0c;可以重载
自定义槽#xff1a;返回值void #xff0c;需要声明#xff0c;也需要实现#xff0c;可以有…第二天
2.1自定义信号和槽
新建一个Qtclass 自定义信号返回值是void 只需要声明不需要实现可以有参数可以重载
自定义槽返回值void 需要声明也需要实现可以有参数可以发生重载
首先新建学生和老师的类。在老师的头文件中添加自定义信号,将代码写在signals:下来实现自定义信号只需要声明不需要实现
class Teacher : public QObject
{Q_OBJECT
public:Teacher(QObject *parent);~Teacher();
//自定义信号写到signals下
//返回值是void 只需要声明不需要实现
//可以有参数可以重载
signals:void hungry();
};在学生类里定义槽返回值void 需要声明也需要实现可以有参数可以发生重载。
class Student : public QObject
{
public:Student(QObject *parent);~Student();
public slots://返回值void 需要声明也需要实现//可以有参数可以发生重载void treat();
};void Student::treat()
{qDebug(请老师吃饭);
}现在weidet头文件里新建老师和学生的指针在到cpp里把他们实例化绑定到父类加入对象树并且使用connect进行连接。光有连接还不行必须要先连接在调用需要调用的时机使用emit来定义触发的时机。写完后就能在输出里看到打印了这段文字。
#include QtWidgets/QWidget
#include ui_widget.h
#include Teacher.h
#include Student.hclass widget : public QWidget
{Q_OBJECT
public:widget(QWidget *parent nullptr);~widget();
private:Ui::widgetClass ui;//先申明两个指针Teacher* te;Student* st;//声明调用函数void classisover();
};#include widget.hwidget::widget(QWidget *parent): QWidget(parent)
{ui.setupUi(this);//创建一个老师对象,右边的this是设置父类加入对象树this-te new Teacher(this);//创建一个学生对象this-st new Student(this);//搭建连接要先连接在传递信号connect(te,Teacher::hungry,st,Student::treat);classisover();
}widget::~widget()
{}void widget::classisover()
{//下课函数调用后出发老师饿了的信号emit te-hungry();
}