亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Qt 信號(hào)自定義槽函數(shù)的實(shí)現(xiàn)

 更新時(shí)間:2021年11月26日 10:54:26   作者:lyshark  
Qt中實(shí)現(xiàn)自定義信號(hào)與槽函數(shù),信號(hào)用于發(fā)送并觸發(fā)槽函數(shù),槽函數(shù)則是具體的功能實(shí)現(xiàn),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下

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++實(shí)現(xiàn)簡(jiǎn)易通訊錄功能

    C++實(shí)現(xiàn)簡(jiǎn)易通訊錄功能

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)易通訊錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++全密碼生成的實(shí)現(xiàn)代碼

    C++全密碼生成的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C++全密碼生成的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C語(yǔ)言 詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度

    C語(yǔ)言 詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度

    算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用: 時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小
    2022-04-04
  • C++深入探究引用的本質(zhì)與意義

    C++深入探究引用的本質(zhì)與意義

    引用是C++一個(gè)很重要的特性,顧名思義是某一個(gè)變量或?qū)ο蟮膭e名,對(duì)引用的操作與對(duì)其所綁定的變量或?qū)ο蟮牟僮魍耆葍r(jià),這篇文章主要給大家總結(jié)介紹了C++中引用的相關(guān)知識(shí)點(diǎn),需要的朋友可以參考下
    2022-04-04
  • C++讀取單個(gè)字符操作示例詳解

    C++讀取單個(gè)字符操作示例詳解

    這篇文章主要為大家介紹了C++讀取單個(gè)字符操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 深入C語(yǔ)言把文件讀入字符串以及將字符串寫(xiě)入文件的解決方法

    深入C語(yǔ)言把文件讀入字符串以及將字符串寫(xiě)入文件的解決方法

    本篇文章是對(duì)C語(yǔ)言把文件讀入字符串以及將字符串寫(xiě)入文件的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • c++自帶的查找函數(shù)詳解

    c++自帶的查找函數(shù)詳解

    這篇文章主要介紹了c++自帶的查找函數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • 一文帶你搞懂C語(yǔ)言動(dòng)態(tài)內(nèi)存管理

    一文帶你搞懂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-11
  • C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例

    C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例

    這篇文章主要介紹了C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 詳解C++中的異常和錯(cuò)誤處理機(jī)制

    詳解C++中的異常和錯(cuò)誤處理機(jī)制

    在C++編程中,異常處理和錯(cuò)誤處理機(jī)制是非常重要的,它們可以幫助程序員有效地處理運(yùn)行時(shí)錯(cuò)誤和異常情況,本文就來(lái)介紹一下C++中的異常處理和錯(cuò)誤處理機(jī)制吧
    2023-05-05

最新評(píng)論