QT中connect高级链接——指针、lambda、宏

1、connect使用指针

connect(button,&QPushButton::released,this,&MainWidget::mySlot); //【抬起】按钮button时,修改按钮b2的标题

2、使用lambda表达式

引入lambda表达式,类似内联函数,可以用于不会被重用的短代码片段,不需要名称,不需要声明。当在 Qt 中配合信号一起使用时,lambda表达式的好处是不用定义槽函数

,也不用指定信号接收者,对于非被重复调用的槽函数起到精简代码的作用。

定义

复制代码
[ https://zhida.zhihu.com/search?content_id=238724677&content_type=Article&match_order=1&q=capture+list&zd_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ6aGlkYV9zZXJ2ZXIiLCJleHAiOjE3NDY3NzI1MTIsInEiOiJjYXB0dXJlIGxpc3QiLCJ6aGlkYV9zb3VyY2UiOiJlbnRpdHkiLCJjb250ZW50X2lkIjoyMzg3MjQ2NzcsImNvbnRlbnRfdHlwZSI6IkFydGljbGUiLCJtYXRjaF9vcmRlciI6MSwiemRfdG9rZW4iOm51bGx9.FVJaO3gAKh73hFDxAUrxQkv8NUxjcYKrdWCsXQXvHCc&zhida_source=entity

 ] (parameters) -> return-type  
{   
   function body
} 

capture list说明

Capture Description
\[\] No capture; Lambda doesn't access any variables from the surrounding scope.
var Capture 'var' by value; Lambda has a copy of 'var' and can use it.
\&var Capture 'var' by reference; Lambda refers to the original 'var'.
= Capture all local variables by value; Lambda has copies of all local variables.
\& Capture all local variables by reference; Lambda refers to all local variables.
this Capture the 'this' pointer; Lambda can access the members of the current object.
var, \&other Mix of capture modes; 'var' is captured by value, 'other' is captured by reference.
=, \&var Mix of capture modes; 'var' is captured by reference, other variables are captured by value.

QT中槽函数

复制代码
connect(&iperf_pro, &QProcess::readyReadStandardOutput, [&]() {
            QByteArray newData = iperf_pro.readAllStandardOutput();
            QString currentText = pnetperf_area->toPlainText();
            currentText += QString::fromLocal8Bit(newData);
            pnetperf_area->setPlainText(currentText);
            //pnetperf_area->append(QString(newData));
        });

lambda表达式开销

由于lambda将生成一个类,因此它的开销将与创建一个包含与捕获的变量数量相同的等效类一样。捕获的变量越多(特别是按值),生成的函数类就越大,使用lambda的成本也就越高。如果通过引用捕获,开销就是几个对应指针的大小。

如果没有捕获任何变量,则它实际上是一个函数调用。如果捕获一个变量,其代价与构造一个对象并直接在其上调用函数相同,而不需要进行虚拟查找。lambda的代价永远不会大于等效函数/类的代价。

connect(button,&QPushButton::released, button()

// 在此处添加mutable关键字,代表传进来的变量可以被修改,不写该关键字则不能被修改

{

if(button->text()=="Lambda表达式") button->setText("表达式Lambda"); //修改按钮标题

else button->setText("Lambda表达式"); //修改按钮标题

qDebug()<<"111111111"; //输出"111111111"

// qDebug()<<a<<b; //当方括号内包含变量a、b或为等号时,此处可以输出变量a、b的值

}

);

// \[\]代表把外部变量传进来,如果不传是不能被使用的

// button代表把变量button传进来

// =代表把外部所有局部变量、类中所有成员以值的传递方式

// this代表把类中所有成员以值的传递方式

// \&代表把外部所有局部变量引用,引用符号

// int a=10,b=100;

// ()第二个参数 函数的参数列表

3、使用宏

connect(sender,SIGNAL(signal()),receiver,SLOT(slot()));

//由于使用宏并不会做错误检查,所以不建议使用

相关推荐
澈20717 小时前
QT入门第十一天:数据库编程(上)SQLite入门与增删改查 | 零基础学QT
数据库·qt·sqlite
用户805533698036 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner6 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz11 天前
QML Hello World 入门示例
qt
xcyxiner14 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner14 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner15 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner16 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能17 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构