Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Custom slot function of Qt signal

Shulou Source: shulou.com Published: 2022-06-02 10:20:51 10月01日 Update

This article shows you how to implement the custom slot function of Qt signal, which is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The custom signal and slot function is realized in Qt, the signal is used to send and trigger the slot function, and the slot function is the specific function realization. As follows, we take teachers and students as examples to simply learn how to use the signal and slot function.

Use non-parametric signals and slots

First, define a teacher class, which is used to send a signal, then the student class, which defines the slot function used to receive the signal, and finally use the emit trigger signal in widget. When the teacher says the class is over, the students treat to dinner.

Only signals need to be defined in teacher.h. Define a void hungry (); signal.

# ifndef TEACHER_H#define TEACHER_H#include class Teacher: public QObject {Q_OBJECTpublic: explicit Teacher (QObject * parent = nullptr); signals: / / define a signal, which must be of void type and cannot implement void hungry ();}; # endif / / TEACHER_H

In student, you need to define the slot declaration and implement the slot.

Student.h

# ifndef STUDENT_H#define STUDENT_H#include class Student: public QObject {Q_OBJECTpublic: explicit Student (QObject * parent = nullptr); signals:public slots: / / Custom slot functions / / Custom slot functions must be defined and must be declared before you can use void treat ();}; # endif / / STUDENT_H

Student.cpp

# include "student.h" # include Student::Student (QObject * parent): the implementation of the QObject (parent) {} / / slot function is as follows: void Student::treat () {qDebug () hungry ();} use parameter signal transmission

It only needs to be improved on the basis of no more parameters.

Widget.cpp

# include "widget.h" Widget::Widget (QWidget * parent): QWidget (parent) {zt = new Teacher (this); st = new Student (this); void (Teacher:: * teacherPtr) (QString) = & Teacher::hungry; void (Student:: * studentPtr) (QString) = & Student::treat; connect (zt,teacherPtr,st,studentPtr); classIsOver () } Widget::~Widget () {} / / trigger signal void Widget::classIsOver () {emit zt- > hungry ("kao'leng'mian cold noodles");}

Student.cpp

# include "student.h" # include Student::Student (QObject * parent): the implementation process of QObject (parent) {} / / slot function is as follows: void Student::treat () {qDebug () setupUi (this); / / use Lambda expression, where [=] takes effect on variables in the file, where [btn] is effective for btn button / / initialization is called once by default, which is a function call determined by (). [=] () {this- > setWindowTitle ("initialize..");} (); / / use mutable to change the variable passed by value int number = 10; QPushButton * btn_ptr1 = new QPushButton ("change variable value", this); btn_ptr1- > move (100100) / / Click the button to change the value of the internal variable, so it will not affect the change of the external variable connect (btn_ptr1,&QPushButton::clicked,this, [=] () mutable {number = number + 100; std::cout

Tags: Signals functions teachers variables students buttons content skills knowledge types processes concise concise that is methods of use examples cold faces functions parameters just need Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Linux Shulou Information Huawei Apple