Qt connect中的 Lambda
来源:互联网 发布:lbp算法matlab 编辑:程序博客网 时间:2024/05/14 06:02
Qt 5 使用 C++11 支持 Lambda 表达式,connect() 的时候如果函数名写错了就会在编译时报错,还有一点是 Lambda 表达式在需要的时候才定义,不需要声明,写起来比较简单,这对于较小的处理函数来说简直太棒了。
Qt connect中使用 Lambda
信号槽
#include <QApplication>#include <QDebug>#include <QPushButton>int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton *button = new QPushButton("点击"); button->show(); QObject::connect(button, &QPushButton::clicked, []() { qDebug() << "点击"; }); return app.exec();}
信号槽(重载)
#include <QApplication>#include <QDebug>#include <QComboBox>int main(int argc, char *argv[]) { QApplication app(argc, argv); QComboBox *comboBox = new QComboBox(); comboBox->addItem("林冲"); comboBox->addItem("鲁达"); comboBox->addItem("武松"); comboBox->show(); QObject::connect(comboBox, &QComboBox::activated, []() { qDebug() << "Hello"; }); return app.exec();}
编译报错: No matching function for call to ‘connect’,原因是信号 QComboBox::activated() 有重载函数:
void QComboBox::activated(int index) void QComboBox::activated(const QString &text)
在进行信号槽绑定时,如果有重载,需要对成员函数进行类型转换,可以使用 C++ 的 static_cast 类型转换(编译时进行语法检查),也可以使用传统的 C 语言的强制类型转换(编译时不进行语法检查,运行时才检查)
#include <QApplication>#include <QDebug>#include <QComboBox>int main(int argc, char *argv[]) { QApplication app(argc, argv); QComboBox *comboBox = new QComboBox(); comboBox->addItem("林冲"); comboBox->addItem("鲁达"); comboBox->addItem("武松"); comboBox->show(); QObject::connect(comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), [](int index) { qDebug() << index; }); QObject::connect(comboBox, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), [](const QString &text) { qDebug() << text; }); return app.exec();}
阅读全文
0 0
- Qt connect中的 Lambda
- QT 使用 lambda来写connect
- qt中的connect函数
- QT中的connect函数
- Qt中的connect函数使用
- Qt Creator中的connect函数
- qt中的connect()函数 信号槽
- 小谈Qt中的connect函数
- Qt使用connect函数时向slot传递参数:使用lambda表达式
- <Qt> 在connect中使用lambda实现高效的信号/槽关联
- qt-connect
- Qt connect
- Qt connect
- C++11 Lambda的变量捕获和Lambda在Qt中的应用
- C++11 Lambda的变量捕获和Lambda在Qt中的应用
- Qt/C++ Lambda 表达式
- Appendix Qt Lambda表达式
- qt connect Qt::ConnectionType介绍
- JavaBean 、标准动作
- SQL Server 2008安装教程
- JS基础之EL表达式
- SSM 框架搭建 mysql
- 微信公众号开发关键词自动回复-微信开发教程9
- Qt connect中的 Lambda
- 二进制中1的个数
- 微信公众号开发流程步骤-微信开发教程10
- 工作管理
- http://www.nvidia.cn/object/what-is-gpu-computing-cn.html
- 使用AsyncTask下载+Service+通知连Notification(控制下载暂停或取消)
- 【动态】IAB科技实验室为广告业成立区块链工作组
- 【观点】以太坊创始人认为区块链技术将在几年内抢走Visa的业务
- 【动态】以太坊Metropolis硬分叉首次迭代将出现