PDA

View Full Version : How to update database with dynamically created widget values



Cyrebo
22nd April 2013, 22:30
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);

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();
}


class FrmBuilder.cpp:



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];
}
}



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.

wysota
23rd April 2013, 00:42
QDataWidgetMapper