Qt5在QObject::connect()添加了幾種新用法,其中一種是lambda表達式(也稱爲:匿名函數),其中C++的lambda表達式原理解說可以參考博主這篇博客:https://blog.csdn.net/naibozhuan3744/article/details/96353035
一、下面是對Qt中在QObject::connect()中的lambda表達式常用用法
QString str("I am a string!");
device=str;
connect(ui->pushButton,&QPushButton::clicked,[&](bool){qDebug()<<"device=="<<device;});
表達式詳解
a1 第一個參數含義
第一個參數ui->pushButton:表示對象;
a2 第二個參數含義
第二個參數&QPushButton::clicked:表示對象包含的信號(該信號隱藏參數,實際上在槽函數仍然可以調用信號參數列表);
a3 第三個參數含義
第三個參數:[&](bool){qDebug()<<"device=="<<device};
該connect只有三個參數,在三個參數情況下,默認第三個槽函數的對象是本類this,也就是第三個參數this被省略了。
[&]中的&:表示調用this作用域內任何參數;一般是全局變量,不能是局部變量,否則局部變量會用完銷燬,調用就會出現內存錯誤,程序奔潰!(注意,[&]這裏前面不用加&,即這種表達式&[&]是錯誤的)
(bool):函數參數列表,該參數列表接收了信號參數列表,也就是等於信號signal的參數列表;
{qDebug()<<"device=="<<device}:函數體,將函數要執行的代碼寫在這裏;
二、具體的實例代碼如下所示
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
QString device;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QString str("I am a string!");
device=str;
connect(ui->pushButton,&QPushButton::clicked,[&](bool){
qDebug()<<"device=="<<device;
/*qDebug()<<"str=="<<str;錯誤用法,會導致程序崩潰,因此局部變量已經銷燬*/});
//connect(ui->pushButton,&QPushButton::clicked,[this](bool){qDebug()<<"device=="<<device;}); //這個也行
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
*.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-09-29T21:45:13
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtProject_lambda
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
結果如下所示
參考內容:
https://www.cnblogs.com/coolcpp/p/cpp-fuction.html(參考:lambda表達式,主要參考!!!)
https://blog.csdn.net/naibozhuan3744/article/details/96353035(參考:lambda表達式原理詳解)
https://blog.csdn.net/cqk0100/article/details/77874142(參考:QObject::connect()的lambda表達式用法)
https://www.cnblogs.com/wanghui1234/p/8964968.html(參考:QObject::connect()的lambda表達式用法,主要參考)