QT_4 (signals and slots)

Function: Click the button to close the window

The theoretical diagram is as follows: Forgotten functions can be searched for code implementation
insert image description here
in the qt assistant
insert image description here

#include "mywidget.h"
#include<QPushButton>//按钮的头文件

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)//初始化列表
{
    
     
    QPushButton * btn=new QPushButton;
    //让btn对象依赖在mywidget窗口中
    btn->setParent(this);
    //显示文本
    btn->setText("123");
    //需求:点击按钮关闭窗口
    //参数1:信号发送者;参数2:发送的信号(函数的地址);参数3:信号接收者;参数4:处理的槽函数
    connect(btn,&QPushButton::clicked,this,&myWidget::close);

}

myWidget::~myWidget()
{
    
    
}


Custom Signals and Slots

Theoretical part:

  1. Custom signal
    1.1 Write to signals
    1.2 Return void, that is, there is no return value
    1.3 Need to declare, but do not need to implement
    1.4 Can have parameters and can be overloaded
  2. Custom slot function
    2.1 Return void
    2.2 Can have parameters, can be overloaded
    2.3 Need to declare, also need to implement
    2.4 Write it under public slot or under public slot
    Actual case:
    Example: The teacher is hungry, and the students please eat
    1.
    First, add students and Teacher header file (.h file)
    studnt.h:
#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

class Student : public QObject
{
    
    
    Q_OBJECT
public:
    explicit Student(QObject *parent = nullptr);
    //槽函数:无返回值,需要声明也需要实现
    //可以有参数可以发生重载
    void treat();

signals:

//有些QT版本里面会有一行这样的代码
    //public slots:
    //这行代码是用来当作槽来使用的,5.4版本后也可以将槽的代码直接写在public下,早期版本一定要写在此处
};

#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 hungry();

};

#endif // TEACHER_H

2.
After setting the header file, set the implementation file (.cpp file)
student.cpp:

#include "student.h"
#include<QDebug>//打印的头文件

Student::Student(QObject *parent) : QObject(parent)
{
    
    

}
void Student::treat()
{
    
    
    qDebug()<<"请老师吃饭";//打印这行字
}

teacher.cpp:

#include "teacher.h"

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

}

Window file mywidget.cpp:

//这是窗口界面
#include "mywidget.h"
#include<QPushButton>//按钮的头文件
#include"teacher.h"
#include"student.h"

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)//初始化列表
{
    
     
   //创建一个老师对象
    this->zt=new Teacher(this);//this(括号里面的)加不加都行,加上了表示对象树就不用考虑释放的问题了
    //创建一个学生对象
    this->st=new Student(this);
    //老师饿了,学生请客的链接;
    connect(zt,&Teacher::hungry,st,&Student::treat);
    //调用下课函数
    classIsOver();


}
void myWidget:: classIsOver()
{
    
    
    //下课函数,调用后触发老师饿了
    emit zt->hungry();
}
myWidget::~myWidget()
{
    
    
}

Remember to add a classIsOver() function definition in the header file of the window file

#ifndef MYWIDGET_H
#define MYWIDGET_H
#include"teacher.h"
#include"student.h"
#include<QWidget>

#include <QWidget>//包含头文件QWidget(窗口类)

class myWidget : public QWidget
{
    
    
    Q_OBJECT//Q_OBJECT宏,允许类中使用信号和槽的机制

public:
    myWidget(QWidget *parent = nullptr);//构造函数
    ~myWidget();
private:
    Teacher *zt;
    Student *st;
    void classIsOver();
};
#endif // MYWIDGET_H

Guess you like

Origin blog.csdn.net/weixin_45866980/article/details/126861550