Qt信号槽Lambda
一、总结
1.Lambda表达式[](){},配合信号与槽使用特别方便,C++11新标准 在.pro 文件里面添加 CONFIG += C++11 之后可以使用。
2.[]这个方括号是用来传递外部变量的, 因为在匿名函数中是无法使用外部变量的, 只能通过方括号传递给他, 才能捕获到. 比如我需要使用a这个变量,
只需要在方括号中加入a即可:[a](){};但是如果要传的变量很多, 一个个加到方括号里就太麻烦了, 这时我们可以使用 '=', 将外部所有局部变量、类中
所有成员以值传递方式传进来: [=] (). 但是通过这种方式传递进来的变量是只读的, 不能修改;
3.使用 this, 可以把类中所有成员以值传递方式传递进来;[this](){}
4.使用 &, 把外部所有局部变量, 通过引用方式传递进来(不推荐, 容易出错);
5.{}花括号内为匿名函数的逻辑代码;
二、代码
#ifndef TESTWIDGET_H #define TESTWIDGET_H #include <QObject> #include <QWidget> #include <QVBoxLayout> #include <QCheckBox> class TestWidget : public QWidget { Q_OBJECT public: explicit TestWidget(QWidget *parent = nullptr); signals: public slots: void doSomething(int num,bool checked); private: void initUI(); }; #endif // TESTWIDGET_H
#include "testwidget.h" #include <QDebug> TestWidget::TestWidget(QWidget *parent) : QWidget(parent) { initUI(); } void TestWidget::initUI() { QVBoxLayout *vLayout=new QVBoxLayout(this); for(int i=0;i<6;i++) { QCheckBox *chk=new QCheckBox(this); chk->setText(QString::number(i)); connect(chk, &QCheckBox::clicked, [=](bool checked){ doSomething(i,checked); }); vLayout->addWidget(chk); } setLayout(vLayout); } void TestWidget::doSomething(int num, bool isOk) { qDebug()<<"num:"<<num<<"isOk:"<<isOk; }
多个信号对应一个槽函数
#ifndef LEDWIDGET_H #define LEDWIDGET_H #include <QWidget> #include <QVBoxLayout> #include <QHBoxLayout> #include <QGroupBox> #include <QGridLayout> #include <QLabel> #include <QLineEdit> #include <QPushButton> namespace Ui { class LedWidget; } class LedWidget : public QWidget { Q_OBJECT public: enum ButtonType{ HIGH_R, HIGH_G, HIGH_B, MIDDLE_R, MIDDLE_G, MIDDLE_B, LOW_R, LOW_G, LOW_B }; explicit LedWidget(QWidget *parent = nullptr); ~LedWidget(); private slots: void slotClickedButton(ButtonType type); private: Ui::LedWidget *ui; void initUI(); QLabel *createLabelText(QString text); QPushButton * newButton(QString text); }; #endif // LEDWIDGET_H
#include "ledwidget.h" #include "ui_ledwidget.h" #include <QDebug> LedWidget::LedWidget(QWidget *parent) : QWidget(parent), ui(new Ui::LedWidget) { ui->setupUi(this); initUI(); } LedWidget::~LedWidget() { delete ui; } void LedWidget::initUI() { QGroupBox *gb=new QGroupBox(this); gb->setTitle("Led"); QPushButton *pbHR=newButton("R"); QPushButton *pbHG=newButton("G"); QPushButton *pbHB=newButton("B"); QPushButton *pbMR=newButton("R"); QPushButton *pbMG=newButton("G"); QPushButton *pbMB=newButton("B"); QPushButton *pbLR=newButton("R"); QPushButton *pbLG=newButton("G"); QPushButton *pbLB=newButton("B"); connect(pbHR,&QPushButton::clicked,[=](){slotClickedButton(HIGH_R);}); connect(pbHG,&QPushButton::clicked,[=](){slotClickedButton(HIGH_G);}); connect(pbHB,&QPushButton::clicked,[=](){slotClickedButton(HIGH_B);}); connect(pbMR,&QPushButton::clicked,[=](){slotClickedButton(MIDDLE_R);}); connect(pbMG,&QPushButton::clicked,[=](){slotClickedButton(MIDDLE_G);}); connect(pbMB,&QPushButton::clicked,[=](){slotClickedButton(MIDDLE_B);}); connect(pbLR,&QPushButton::clicked,[=](){slotClickedButton(LOW_R);}); connect(pbLG,&QPushButton::clicked,[=](){slotClickedButton(LOW_G);}); connect(pbLB,&QPushButton::clicked,[=](){slotClickedButton(LOW_B);}); QGridLayout *gLayout=new QGridLayout(); gLayout->addWidget(createLabelText("High:"),0,0); gLayout->addWidget(pbHR,0,1); gLayout->addWidget(pbHG,0,2); gLayout->addWidget(pbHB,0,3); gLayout->addWidget(createLabelText("Middle:"),1,0); gLayout->addWidget(pbMR,1,1); gLayout->addWidget(pbMG,1,2); gLayout->addWidget(pbMB,1,3); gLayout->addWidget(createLabelText("Low:"),2,0); gLayout->addWidget(pbLR,2,1); gLayout->addWidget(pbLG,2,2); gLayout->addWidget(pbLB,2,3); gb->setLayout(gLayout); QVBoxLayout *vLayoutMain=new QVBoxLayout(this); vLayoutMain->addWidget(gb); setLayout(vLayoutMain); } QLabel *LedWidget::createLabelText(QString text) { QLabel *lbl=new QLabel(text,this); lbl->setMinimumSize(80,25); lbl->setMaximumSize(80,25); return lbl; } QPushButton *LedWidget::newButton(QString text) { QPushButton * pb=new QPushButton(text,this); pb->setMinimumSize(30,30); pb->setMaximumSize(30,30); if(text=="R") { pb->setObjectName("pbRed"); } else if(text=="G") { pb->setObjectName("pbGreen"); } else if(text=="B") { pb->setObjectName("pbBlue"); } return pb; } void LedWidget::slotClickedButton(LedWidget::ButtonType type) { qDebug()<<type; if(type==HIGH_R) { } else if(type==HIGH_G) { } else if(type==HIGH_B) { } else if(type==MIDDLE_R) { } else if(type==MIDDLE_G) { } else if(type==MIDDLE_B) { } else if(type==LOW_R) { } else if(type==LOW_G) { } else if(type==LOW_B) { } }