Hi again I know its been a while because I had to do some other things but I still don't know how to use the values created from the dynamic widgets. This is the updated version of 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();
}
MainWindow::~MainWindow()
{
delete fb;
delete ui;
}
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();
}
MainWindow::~MainWindow()
{
delete fb;
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
fb->display();
}
To copy to clipboard, switch view to plain text mode
class FrmBuilder:
QList<int> values;
FrmBuilder::FrmBuilder(Ui::MainWindow *ui)
{
this->myUi = ui;
}
void FrmBuilder::buildFrm()
{
if (qry.exec("SELECT name FROM persons"))
{
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];
}
}
QList<int> values;
QLabel *label;
FrmBuilder::FrmBuilder(Ui::MainWindow *ui)
{
this->myUi = ui;
}
void FrmBuilder::buildFrm()
{
QSqlQuery qry;
if (qry.exec("SELECT name FROM persons"))
{
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
Bookmarks