Qt 信號(hào)自定義槽函數(shù)的實(shí)現(xiàn)
Qt中實(shí)現(xiàn)自定義信號(hào)與槽函數(shù),信號(hào)用于發(fā)送并觸發(fā)槽函數(shù),槽函數(shù)則是具體的功能實(shí)現(xiàn),如下我們以老師學(xué)生為例子簡(jiǎn)單學(xué)習(xí)一下信號(hào)與槽函數(shù)的使用方法。
使用無(wú)參數(shù)信號(hào)與槽
首先定義一個(gè)teacher類,該類中用于發(fā)送一個(gè)信號(hào),其次student類,定義用于接收該信號(hào)的槽函數(shù),最后在widget中使用emit觸發(fā)信號(hào),當(dāng)老師說(shuō)下課時(shí),學(xué)生請(qǐng)客吃飯。
teacher.h 中只需要定義信號(hào)。定義一個(gè) void hungry(); 信號(hào)。
#ifndef TEACHER_H #define TEACHER_H #include <QObject> class Teacher : public QObject { Q_OBJECT public: explicit Teacher(QObject *parent = nullptr); signals: // 定義一個(gè)信號(hào),信號(hào)必須為void類型,且信號(hào)不能實(shí)現(xiàn) void hungry(); }; #endif // TEACHER_H
student中需要定義槽聲明,并實(shí)現(xiàn)槽。
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: // 自定義槽函數(shù) // 槽函數(shù)必須定義且必須要聲明才可以使用 void treat(); }; #endif // STUDENT_H
student.cpp
#include "student.h" #include <QDebug> Student::Student(QObject *parent) : QObject(parent) { } // 槽函數(shù)的實(shí)現(xiàn)過(guò)程如下 void Student::treat() { qDebug() << "請(qǐng)老師吃飯"; }
Widget.h定義信號(hào)發(fā)送函數(shù),與類
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "student.h" #include "teacher.h" class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); // 定義學(xué)生與老師類 Teacher *zt; Student *st; // 定義信號(hào)發(fā)送函數(shù) void classIsOver(); }; #endif // WIDGET_H
Widget.cpp 具體實(shí)現(xiàn)
#include "widget.h" Widget::Widget(QWidget *parent): QWidget(parent) { zt = new Teacher(this); st = new Student(this); // zt向st發(fā)送信號(hào),信號(hào)是&Teacher::hungry 處理槽函數(shù)是 &Student::treat connect(zt,&Teacher::hungry,st,&Student::treat); classIsOver(); } Widget::~Widget() { } // 觸發(fā)信號(hào) void Widget::classIsOver() { emit zt->hungry(); }
使用有參信號(hào)傳遞
只需要再無(wú)參基礎(chǔ)上改進(jìn)
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() { } // 觸發(fā)信號(hào) void Widget::classIsOver() { emit zt->hungry("kao'leng'mian烤冷面"); }
student.cpp
#include "student.h" #include <QDebug> Student::Student(QObject *parent) : QObject(parent) { } // 槽函數(shù)的實(shí)現(xiàn)過(guò)程如下 void Student::treat() { qDebug() << "請(qǐng)老師吃飯"; } void Student::treat(QString foodName) { qDebug() << "請(qǐng)老師吃飯了!,老師要吃:" << foodName.toUtf8().data() ; }
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: // 自定義槽函數(shù) // 槽函數(shù)必須定義且必須要聲明才可以使用 void treat(); void treat(QString); }; #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: // 定義一個(gè)信號(hào),信號(hào)必須為void類型,且信號(hào)不能實(shí)現(xiàn) void hungry(); void hungry( QString foodName ); }; #endif // TEACHER_H
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QString> #include <QPushButton> #include "student.h" #include "teacher.h" class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); // 定義學(xué)生與老師類 Teacher *zt; Student *st; // 定義信號(hào)發(fā)送函數(shù) void classIsOver(); }; #endif // WIDGET_H
點(diǎn)擊按鈕觸發(fā)信號(hào)
當(dāng)我們點(diǎn)擊按鈕時(shí),自動(dòng)觸發(fā)信號(hào)。只需需改widget中的內(nèi)容。
Widget::Widget(QWidget *parent): QWidget(parent) { st = new Student(this); tt = new Teacher(this); QPushButton *btn = new QPushButton("發(fā)送郵件",this); void(Teacher:: *teacherPtr)(void) = &Teacher::send_mail; void(Student:: *studentPtr)(void) = &Student::read_mail; // 將btn綁定到button上,點(diǎn)擊后觸發(fā)tt 里面的teacherPtr -> 產(chǎn)生信號(hào)send_mail; connect(btn,&QPushButton::clicked,tt,teacherPtr); // 接著將產(chǎn)生信號(hào)綁定到 st 里面的student -> 也就是read_mail槽函數(shù)上。 connect(tt,teacherPtr,st,studentPtr); }
匿名函數(shù)與槽
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); // 使用Lambda表達(dá)式,其中的[=] 對(duì)文件內(nèi)的變量生效,其中[btn]則是對(duì)btn按鈕生效 // 默認(rèn)會(huì)調(diào)用一次完成初始化,這是由()決定的函數(shù)調(diào)用。 [=](){ this->setWindowTitle("初始化.."); }(); // 使用mutable可以改變通過(guò)值傳遞的變量 int number = 10; QPushButton *btn_ptr1 = new QPushButton("改變變量值",this); btn_ptr1->move(100,100); // 點(diǎn)擊按鈕改變內(nèi)部變量的值,由于值傳遞所以不會(huì)影響外部變量的變化 connect(btn_ptr1,&QPushButton::clicked,this,[=]()mutable{ number = number + 100; std::cout << "inner: " << number << std::endl; }); // 當(dāng)點(diǎn)擊以下按鈕時(shí)number還是原值 QPushButton *btn_ptr2 = new QPushButton("測(cè)試值傳遞",this); btn_ptr2->move(100,200); connect(btn_ptr2,&QPushButton::clicked,this,[=](){ std::cout << "The Value: " << number << std::endl; }); // 使用Lambda表達(dá)式返回值,有時(shí)存在返回的情況 int ref = []()->int{ return 1000; }(); std::cout << "Return = " << ref << std::endl; }
到此這篇關(guān)于Qt 信號(hào)自定義槽函數(shù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Qt 信號(hào)自定義槽函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言 詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度
算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用: 時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小2022-04-04深入C語(yǔ)言把文件讀入字符串以及將字符串寫(xiě)入文件的解決方法
本篇文章是對(duì)C語(yǔ)言把文件讀入字符串以及將字符串寫(xiě)入文件的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05一文帶你搞懂C語(yǔ)言動(dòng)態(tài)內(nèi)存管理
動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存。本文將通過(guò)幾個(gè)示例帶大家深入了解一下C語(yǔ)言的動(dòng)態(tài)內(nèi)存管理,需要的可以參考一下2022-11-11C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例
這篇文章主要介紹了C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07