kish69
kish69

Reputation: 21

How to extract a value from a lambda connection

Once the user presses 'PBGen', SBMat and PBGen disappear, so I tried to collect the value of SBMat in the variable 'ent' but it crash. Might you help me?

#include "wmenu.h"
#include <ui_Menu.h>
#include <QPushButton>
#include <QSpinBox>

WMenu::WMenu(QWidget *parent) : QWidget(parent) , Pre(new Ui::Principal)
{
    
    Pre->setupUi(this);
    int ent=0;

    connect(Pre->PBGen,(&QPushButton::clicked),[=,&ent]()
    {
            Pre->SBMat->hide();
            Pre->PBGen->hide();
            ent=Pre->SBMat->value();
          
    });

}

WMenu::~WMenu()
{
}

Upvotes: 1

Views: 44

Answers (1)

declare ent as a class member:

class WMenu....
{
    ....
    private:
    ...
    ...
    int ent{};
}

and in the constructor do:

WMenu::WMenu(QWidget *parent) : QWidget(parent) , Pre(new Ui::Principal)
{        
    Pre->setupUi(this);
    //int ent=0;
    connect(Pre->PBGen,(&QPushButton::clicked),[this]()
    {
            Pre->SBMat->hide();
            Pre->PBGen->hide();
            ent=Pre->SBMat->value();              
    });

}

Upvotes: 1

Related Questions