Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Custom slot function of Qt signal

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report