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()));

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

相关推荐
秋田君11 小时前
QT_JSON文件操作
开发语言·qt·json
大白同学42111 小时前
初识Qt——信号和槽
开发语言·qt
秋田君12 小时前
QT_INI文件操作
开发语言·qt
≮傷£≯√13 小时前
FFmpeg + qt 音频播放
qt·ffmpeg·音视频
youqingyike13 小时前
【无标题】
前端·qt
程序员老陆13 小时前
Qt::WidgetAttribute 常用属性详解
开发语言·qt
程序员老陆1 天前
Qt的QThread::usleep和FFmpeg的libavutil模块的av_usleep哪个精度高一些?
开发语言·qt·ffmpeg·音视频
Quz1 天前
QML TextArea 入门(二):富文本与换行模式
qt
大白同学4211 天前
初识Qt——创建第一个Qt项目
开发语言·qt
秋田君1 天前
QT_绘图原理双缓冲机制
服务器·数据库·qt