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

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

相关推荐
new_zhou16 分钟前
QT5使用cmakelists引入Qt5Xlsx库并使用
开发语言·qt·qxlsx
笨笨马甲2 小时前
Qt 3D模块加载复杂模型
开发语言·qt·3d
赟赟、嵌入式9 小时前
imx6ul Qt运行qml报错This plugin does not support createPlatformOpenGLContext!
开发语言·qt
EutoCool11 小时前
Qt窗口:菜单栏
开发语言·c++·嵌入式硬件·qt·前端框架
云空20 小时前
《QtPy:Python与Qt的完美桥梁》
开发语言·python·qt·pyqt
墨月白21 小时前
【QT】多线程相关教程
数据库·qt
Mr_Xuhhh1 天前
QWidget的属性
java·数据库·c++·qt·系统架构
苏克贝塔1 天前
Qt 图形视图框架4-动画、碰撞检测和图形项组
开发语言·qt
EutoCool1 天前
Qt:布局管理器Layout
开发语言·c++·windows·嵌入式硬件·qt·前端框架
真的想上岸啊2 天前
学习C++、QT---21(QT中QFile库的QFile读取文件、写入文件的讲解)
c++·qt·学习