Qt5 中使用 Lamda 表达式

例1

    qDebug() << []() { return "hello world!";}();
    qDebug() << [](int a) { return a;}(3);

例2

     connect(radioGroup, 
     QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), 
     [=](QAbstractButton *button, bool checked){ 
     qDebug() << "button is: " << button << " is checked: " << checked; 
     });

解释:

  • [ ] :捕捉列表
  • ( ) : 参数列表
  • mutable : 修饰符
  • -> 后面跟返回类型
  • {} :函数体

延申阅读:

  • Qt5中使用lambda表达式
    https://www.cnblogs.com/yuankaituo/p/5135750.html
  • C++11 新特性:Lambda 表达式
    https://www.devbean.net/2012/05/cpp11-lambda/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.