Passing variables while function is triggered by a slot
-
Hi. Per my understanding, many function calls in Qt can be replaced with signals and slots. I have the following code:
void TestConfig::on_pushButton_New_clicked() { inputBox = new QDialog(this); //Set size of QDialog. QGridLayout *gridLayout = new QGridLayout(this); inputBox->setLayout(gridLayout); //QLabel QLabel *boxText = new QLabel(this); boxText->setText("Input new test presets:"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); boxText->setAlignment(Qt::AlignCenter); gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter); //Add a QLineEdit nameInput = new QLineEdit; gridLayout->addWidget(nameInput, 1, 1); //Add a QLineEdit startTempInput = new QLineEdit; gridLayout->addWidget(startTempInput, 2, 1); QLineEdit *expFlashptInput = new QLineEdit; gridLayout->addWidget(expFlashptInput, 3, 1); //Add a QDialogButtonBox QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this); gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter); //Read information connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig())); connect(boxButton, SIGNAL(accepted()), inputBox, SLOT(accept())); } void TestConfig::setTestConfig(){ qDebug() << "In sTestC"; int startTemp = startTempInput->text().toInt(); int expFlashptTemp = expFlashptInput->text().toInt(); qDebug() << startTemp; qDebug() << endTemp; ConfigLibrarian librarian; ConfigurationSet config; config.name = nameInput->text(); config.serialNum = "desktop"; config.startTemp = startTempInput->text().toInt(); config.endTemp = 86; config.tempRate = 5.5; config.igniteFreq = 10; config.pressureThresh = 20; config.minTempAboveIni = 10; }
In this code in order to pass data in startTempInput and expFlashptInput through, I had to declare them out of the function, which is something I am trying to avoid since the class they are built in is getting bigger. I was wondering whether there is a way to pass variables through to a slot when triggered by a signal.
I am aware that in the documentation it is listed I can't do this:
connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed(QObject*)));
Hence, I am wondering whether a workaround that doesn't involve declaring the variables in the class.Please let me know if more info is required.
-
Hi. Per my understanding, many function calls in Qt can be replaced with signals and slots. I have the following code:
void TestConfig::on_pushButton_New_clicked() { inputBox = new QDialog(this); //Set size of QDialog. QGridLayout *gridLayout = new QGridLayout(this); inputBox->setLayout(gridLayout); //QLabel QLabel *boxText = new QLabel(this); boxText->setText("Input new test presets:"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); boxText->setAlignment(Qt::AlignCenter); gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter); //Add a QLineEdit nameInput = new QLineEdit; gridLayout->addWidget(nameInput, 1, 1); //Add a QLineEdit startTempInput = new QLineEdit; gridLayout->addWidget(startTempInput, 2, 1); QLineEdit *expFlashptInput = new QLineEdit; gridLayout->addWidget(expFlashptInput, 3, 1); //Add a QDialogButtonBox QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this); gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter); //Read information connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig())); connect(boxButton, SIGNAL(accepted()), inputBox, SLOT(accept())); } void TestConfig::setTestConfig(){ qDebug() << "In sTestC"; int startTemp = startTempInput->text().toInt(); int expFlashptTemp = expFlashptInput->text().toInt(); qDebug() << startTemp; qDebug() << endTemp; ConfigLibrarian librarian; ConfigurationSet config; config.name = nameInput->text(); config.serialNum = "desktop"; config.startTemp = startTempInput->text().toInt(); config.endTemp = 86; config.tempRate = 5.5; config.igniteFreq = 10; config.pressureThresh = 20; config.minTempAboveIni = 10; }
In this code in order to pass data in startTempInput and expFlashptInput through, I had to declare them out of the function, which is something I am trying to avoid since the class they are built in is getting bigger. I was wondering whether there is a way to pass variables through to a slot when triggered by a signal.
I am aware that in the documentation it is listed I can't do this:
connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed(QObject*)));
Hence, I am wondering whether a workaround that doesn't involve declaring the variables in the class.Please let me know if more info is required.
You can use a lambda for that.
connect(boxButton, &QButtonBox::accepted, this, [this, startTempInput, expFlashptInput]() { int startTemp = startTempInput->text().toInt(); int expFlashptTemp = expFlashptInput->text().toInt(); qDebug() << startTemp; qDebug() << endTemp; // ... });