日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++笔记-Qt中使用Lambda时[]中的形式

發布時間:2025/3/15 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++笔记-Qt中使用Lambda时[]中的形式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有幾個地方要注意的:

[]這個表示Lambda的開始,如果要加參數可以這樣:[]()后面括號里面放參數,Qt中connect中的信號,參數

1. []:里面為空,表示不使用任何參數對象的參數;

2. =:表示按值的方式進行傳遞;

3. &:表示以引用的方式進行傳遞;

4. this:表示函數體內可以使用Lambda所在類中的成員變量;

5. a:按值的方式進行傳遞,默認是不能修改的,如果要修改,需要添加mutable修飾符。

?

程序結構如下:

如下代碼:

LambdaInQt.pro

QT -= guiCONFIG += c++11 console CONFIG -= app_bundle# The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp# Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += targetHEADERS += \Test.h

Test.h

#ifndef TEST_H #define TEST_H#include <QObject> #include <QTimer> #include <QDebug>class Test1{public:Test1(){}Test1(const Test1 &test){this->m_a = test.m_a;}Test1 &operator = (const Test1 &test){this->m_a = test.m_a;}int m_a; };class Test2{public:int m_a; };class MyEmit : public QObject{Q_OBJECTpublic:MyEmit() : QObject(nullptr){QTimer::singleShot(1000, [this](){this->m_test1.m_a = 10;this->m_test2.m_a = 20;});Test1 tmpTest1;tmpTest1.m_a = 100;QTimer::singleShot(1000, [=](){this->m_a = 100;this->m_test1.m_a = 100;});QTimer::singleShot(1000, [&](){this->m_a = 100;tmpTest1.m_a = 300;});int tmpa = 100;QTimer::singleShot(1000, [tmpa](){qDebug() << tmpa;});QTimer::singleShot(1000, [tmpTest1](){qDebug() << tmpTest1.m_a;});}signals:void sendSomeThing();void sendToDoSomeThing();private:int m_a;int m_b;Test1 m_test1;Test2 m_test2; };#endif // TEST_H

main.cpp

#include <QCoreApplication> #include <QDebug> #include <QTimer> #include "Test.h"int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);MyEmit myEmit;QTimer::singleShot(3000, [&]{emit myEmit.sendSomeThing();});QEventLoop loop;QObject::connect(&myEmit, &MyEmit::sendSomeThing, [=](){});loop.exec();qDebug() << "over";return a.exec(); }

?

?

總結

以上是生活随笔為你收集整理的C++笔记-Qt中使用Lambda时[]中的形式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。