Hi, I don't how to use the values from spinboxes that are dynamically created at runtime. I want to get the current value in the spinboxes when I click a push button.
Here's what I've done so far:
if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
int value;
if (qry.exec("SELECT name FROM customer"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
if(qry.isValid())
{
QString cust
= qry.
record().
value(0).
toString();
//create widgets
spinbox->setMaximum(3);
label->setGeometry(0,0,80,41);
//add to VBoxLayout
ui->verticalLayout->addWidget(label);
ui->verticalLayout->addWidget(spinbox);
value = spinbox->value(); // value is always going to be zero here
qDebug() << value;
}
}
}
else
{
qDebug() << qry.lastError();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
//get spinbox values
if(!db.isOpen()){
qDebug() << "No connection to db";
return;
}
int value2;
value2 = spinbox->value2();
qDebug() << value2;
//save values to db
............
QFileInfo checkFile(Path_to_DB);
if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
int value;
QSqlQuery qry;
if (qry.exec("SELECT name FROM customer"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
if(qry.isValid())
{
QString cust = qry.record().value(0).toString();
//create widgets
QLabel *label = new QLabel(QString(cust));
QSpinBox *spinbox = new QSpinBox;
spinbox->setMaximum(3);
label->setGeometry(0,0,80,41);
//add to VBoxLayout
ui->verticalLayout->addWidget(label);
ui->verticalLayout->addWidget(spinbox);
value = spinbox->value(); // value is always going to be zero here
qDebug() << value;
}
}
}
else
{
qDebug() << qry.lastError();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
//get spinbox values
if(!db.isOpen()){
qDebug() << "No connection to db";
return;
}
int value2;
value2 = spinbox->value2();
qDebug() << value2;
//save values to db
............
To copy to clipboard, switch view to plain text mode
Thanks in advance!
--
Bookmarks