PDA

View Full Version : Pass data on button click



prabhatjha
21st January 2020, 16:33
Hello Everyone,

I have a widget with one LineEdit and one save button. On save button click i am calling a slot and in slot i need the data which i enter in LineEdit.
But i am not able to get the data in slot function which i have enetered in lineedit. Thanx in advance.

QWidget *Thresholdwindow = new QWidget;
QLabel *label = new QLabel("Threshold Input");
QLineEdit *lineedit = new QLineEdit(Thresholdwindow);
QPushButton *Save = new QPushButton("Save");
QPushButton *Cancel = new QPushButton("Cancel");
QFormLayout *layout = new QFormLayout;
layout->addRow(label, lineedit);
layout->addRow(Save, Cancel);
Thresholdwindow->setLayout(layout);
int thresholdval = lineedit->text().toInt
connect(Save, SIGNAL(clicked()), this, SLOT(saveThreshold(thresholdval)));

void MainWindow::saveThreshold(int val)
{
qDebug() << val ;
}

d_stranz
21st January 2020, 19:05
You don't say anything about where you have defined this list of variables. If they are defined at class scope (eg. as members of the MainWindow class), then in your slot that handles the clicked() signal, you can simply access the value of "thresholdval".

Your slot to handle the clicked() signal must have the same signature as the signal itself:



MainWindow::MainWindow()
{
// ...
connect(Save, SIGNAL(clicked()), this, SLOT(onClicked()));
}

void MainWindow::onClicked()
{
someFunctionToSaveTheValue( thresholdval );
}


Of course, "thresholdval" must be a member variable of the MainWindow class. If you define it as a local variable within a method, then it has no visibility outside that method.