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

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

相关推荐
kaixin_learn_qt_ing4 小时前
Qt Model/View
qt
Quz5 小时前
QML MouseArea:鼠标拖拽与滚轮缩放
qt·qml
CHPCWWHSU7 小时前
DeepSeek-Harness-Qt将浏览器端Agent装入桌面应用
开发语言·qt
飘忽不定的bug11 小时前
ubuntu20.04交叉编译QT5.15.12(RK3562+ubuntu20.04)
linux·qt·ubuntu
秋田君13 小时前
Qt_QMediaPlayer类与QMediaPlaylist类
开发语言·qt
mengzhi啊13 小时前
QT程序界面自适应1080/2K/4K屏幕。qputenv或者setAttribute
qt
C++ 老炮儿的技术栈13 小时前
Qt5 使用 QPainter 绘制阿基米德螺线
开发语言·c++·windows·qt·代码化
大丑哥1 天前
QT QML 中实现高亮功能
qt
小白不想画工图1 天前
银河麒麟V10系统安装QT5.12
linux·开发语言·qt·银河麒麟
mengzhi啊1 天前
QEventLoop loop配合loop.exec();替代while(1)。电脑cpu占用为0,使用while(1)cpu占用率100%
c++·qt