Qt Learning Tour - Signals and Slots

Click to close window

connect(信号发送者,发送的具体信号,信号接收者,型号的处理(槽slot));

The custom ones here MyPushButtonare like QPushButtonno other

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    
    
    resize(600,400);
    setWindowTitle("第一个窗口");
    setFixedSize(600,400);
    //创建一个按钮
    MyPushButton *myBtn = new MyPushButton;
    myBtn->setText("我自己的按钮");
    myBtn->setParent(this);
    myBtn->move(0,20);

    //需求: 点击我的按钮 关闭窗口
    connect(myBtn,&QPushButton::clicked, this,&Widget::close);
}

Custom Signals and Slots

insert image description here

//teacher.h
#ifndef TEACHER_H
#define TEACHER_H

#include <QObject>

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

signals:
    //自定义信号写到signals下
    //返回值是void 只需要申明,不需要实现
    //可以有参数,可以重载
    void hungry();

public slots:
};

#endif // TEACHER_H

//student.h
#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

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

signals:

public slots:
    //早期Qt版本 必须要写到public slots,高级版本可以写到public或者全局下
    //返回值void,需要声明,也需要实现
    //可以有参数,可以发生重载
    void treat(); //申明
};

#endif // STUDENT_H

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

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

}

void Student::treat(){
    
    
  //实现
    qDebug() << "请老师吃饭";
}

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

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

namespace Ui {
    
    
class Widget;
}

class Widget : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
    Teacher *zt;
    Student *stu;

	void classIsOver();
};

#endif // WIDGET_H

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



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

    //创建一个老师对象
    this->zt = new Teacher(this);
    //创建一个学生对象
    this->stu = new Student(this);
    //老师触发一个信号:饿了;学生响应信号:请客吃饭
    connect(zt,&Teacher::hungry,stu,&Student::treat);
    //调用下课函数
    classIsOver();
}

void Widget::classIsOver(){
    
    
    //触发信号
    emit zt->hungry();
}

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

Customize signals and slots to solve overloading problems

//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();
    void hungry(QString foodName); //添加

public slots:
};

#endif // TEACHER_H
//student.h
#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

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

signals:

public slots:
    void treat();
    void treat(QString foodName); //添加
};

#endif // STUDENT_H

//student.cpp(实现)
#include "student.h"
#include <QDebug>

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

}

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

void Student::treat(QString foodName){
    
    
    qDebug() << "请老师吃饭,老师要吃" << foodName;
}

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



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

    //创建一个老师对象
    this->zt = new Teacher(this);
    //创建一个学生对象
    this->stu = new Student(this);
    //连接带参数的信号和槽
    //指针 -> 地址
    //函数指针 -> 函数地址
    void(Teacher:: *teacherSignal)(QString) = &Teacher::hungry;//指向有参数的那个
    void(Student:: *studentSlot)(QString) = &Student::treat; //指向有参数的那个
    connect(zt,teacherSignal,stu,studentSlot);
    //调用下课函数
    classIsOver();
}
void Widget::classIsOver(){
    
    
    //触发信号
    emit zt->hungry("红烧牛肉");
}

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

Need to tell it which function definition to find through the function pointer (with or without parameters)

insert image description here
""There's not something to eat here (yes "红烧牛肉")

//Student.cpp
void Student::treat(QString foodName){
    
    
    qDebug() << "请老师吃饭,老师要吃" << foodName.toUtf8().data();
}

Key point: need to use the function pointer to clearly point to the address of the functionvoid(类名:: *实例名)(QString/*形参*/) = 类名::重载函数名

  • QString intochar *
    • toUtf8() is converted to QByteArray; and then used to data()convert tochar *

Signals and Connection Signals

Click a button to trigger a custom connection

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

    //创建一个老师对象
    this->zt = new Teacher(this);
    //创建一个学生对象
    this->stu = new Student(this);
    //连接带参数的信号和槽
    //指针 -> 地址
    //函数指针 -> 函数地址
    void(Teacher:: *teacherSignal)(QString) = &Teacher::hungry;//指向有参数的那个
    void(Student:: *studentSlot)(QString) = &Student::treat; //指向有参数的那个
    connect(zt,teacherSignal,stu,studentSlot);
    //重置窗口大小
    this->resize(600,400);
    QPushButton *btn = new QPushButton("下课",this);
    //connect(btn,&QPushButton::clicked,this,&Widget::classIsOver);
	connect(btn,&QPushButton::clicked,zt,teacherSignal);

}

The signal is still calling the slot function

Disconnect

//void(Teacher:: *teacherSignal)(QString) = &Teacher::hungry;//指向有参数的那个
//void(Student:: *studentSlot)(QString) = &Student::treat; //指向有参数的那个
//connect(zt,teacherSignal,stu,studentSlot);
disconnect(zt,teacherSignal,stu,studentSlot);//与连接参数一致

Qt4 version signal slot connection

connect(zt,SUGHAL(hungry),st,SLOT(treat()));

Lambda expression

Lambda expressions in C++11 are used to define and create anonymous function objects . to simplify programming

insert image description here
Format

[](){
    
    }();
  • Empty, no function object arguments are used.
  • =, the body of the function can use all the local variables in the scope of the Lambda (including the this of the Lambda) and yes 引用传递方式(equivalent to the compiler automatically passing all the local variables by reference for us)
  • &, the function body can use lambdaall visible local variables (including the this of the class where the lambda is located) in the return function, and yes 引用传递方式(equivalent to the compiler automatically passing all local variables by reference for us)
  • this, lambdathe member variables in the class can be used in the function body
  • a, Pass a by value. When passing by value, the copy of a passed in cannot be modified in the function body, because the function is const by default. To modify the copy of a passed in , mutablemodifiers can be added.
  • &a, passing a by reference.
  • a,&b: pass a by value and b by reference
  • =,&a,&bExcept that a and b are passed by reference, other parameters are passed by value
  • &,a,bExcept that a and b are passed by value, other parameters are passed by reference.

Guess you like

Origin blog.csdn.net/yasinawolaopo/article/details/131073687