I want to update my database with values from widgets that are created dynamically. I can get the values from the widgets but to link and update dynamic values with the attributes in my database is the problem.
Here is my code:
{
ui->setupUi(this);
fb = new FrmBuilder(ui);
if(checkFile.isWritable())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
fb->buildFrm();
}
void MainWindow::on_pushButton_clicked()
{
fb->display();
}
{
ui->setupUi(this);
fb = new FrmBuilder(ui);
QFileInfo checkFile(Path_to_DB);
if(checkFile.isWritable())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
fb->buildFrm();
}
void MainWindow::on_pushButton_clicked()
{
fb->display();
}
To copy to clipboard, switch view to plain text mode
class FrmBuilder.cpp:
void FrmBuilder::buildFrm()
{
if (qry.exec("SELECT persons FROM candidate"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
QString person
= qry.
record().
value(0).
toString();
spinBoxes << spinbox;
label->setGeometry(0,0,80,41);
for(int i = 0; i<spinBoxes.size();i++){
myUi->verticalLayout->addWidget(label);
myUi->verticalLayout_2->addWidget(spinBoxes[i],0,0);
}
}
}
else
{
qDebug() << qry.lastError();
}
qry.clear();
}
void FrmBuilder::display()
{
for(int i = 0; i<spinBoxes.size(); i++){
values << spinBoxes[i]->value();
qDebug() << values[i];
}
}
void FrmBuilder::buildFrm()
{
QSqlQuery qry;
if (qry.exec("SELECT persons FROM candidate"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
QString person = qry.record().value(0).toString();
label = new QLabel(QString(person));
spinbox = new QSpinBox;
spinBoxes << spinbox;
label->setGeometry(0,0,80,41);
for(int i = 0; i<spinBoxes.size();i++){
myUi->verticalLayout->addWidget(label);
myUi->verticalLayout_2->addWidget(spinBoxes[i],0,0);
}
}
}
else
{
qDebug() << qry.lastError();
}
qry.clear();
}
void FrmBuilder::display()
{
for(int i = 0; i<spinBoxes.size(); i++){
values << spinBoxes[i]->value();
qDebug() << values[i];
}
}
To copy to clipboard, switch view to plain text mode
So that code basically creates a widget form with a label and spinbox based on dynamic user line edits. Reads the values from the created form and prints them.
I just want to know how I will link this value to the specific label (person) in my database so it increments it.
Bookmarks