PDA

View Full Version : MySql data not shown in QcomboBox



diyanafadzil
17th September 2015, 05:07
Hello everyone.

I seem to have problem regarding on how to show or technically make the mysql data visible in the qcombobox.

This is how my gui looks like:
11377

After I saved the details in each field, the details will be shown in mysql database which in this case I used phpmyadmin (XAMPP).

Then I want to retrieve the data by using FaceID and when I click LOAD DATA button, all the FaceID will be in the combo box. However, the problem is it become like this. I have debug and the data is there, but it did not show in the combo box.

11378

This is my code :


void FormDialog::on_comboBox_currentIndexChanged(const QString &arg1)
{

connOpen();

Face = ui->comboBox->currentText();

QSqlQuery qry;

qry.prepare("SELECT * FROM templateinfo WHERE FaceID='"+Face+"'");

if(qry.exec())
{
while(qry.next())
{
ui ->FaceIDLineEdit->setText(qry.value(0).toString());
ui ->nameLineEdit->setText(qry.value(1).toString());
ui ->female->setChecked(qry.value(2).toBool());
ui ->male->setChecked(qry.value(3).toBool());
ui ->WantedlineEdit->setText(qry.value(4).toString());
qDebug() << "execute";
}

connClose();
}

else {
qDebug() << "not execute";
}
}




void FormDialog::on_loadData_clicked()
{
connOpen();
QSqlQueryModel * modal = new QSqlQueryModel();
QSqlQuery* qry = new QSqlQuery(db);
qry->prepare("SELECT FaceID FROM templateinfo");
qry->exec();
modal->setQuery(*qry);

ui->comboBox->setModel(modal);

qry->next();
qDebug() << (modal->rowCount());

}


Please help me solve this problem as I am new in Qt and I dont know which part is wrong :(

anda_skoa
17th September 2015, 09:04
qry.prepare("SELECT * FROM templateinfo WHERE FaceID='"+Face+"'");


Don't concatenate values onto an QSL query, use QSqlQuery::bindValue() instead.





void FormDialog::on_loadData_clicked()
{
connOpen();
QSqlQueryModel * modal = new QSqlQueryModel();
QSqlQuery* qry = new QSqlQuery(db);
qry->prepare("SELECT FaceID FROM templateinfo");
qry->exec();
modal->setQuery(*qry);

ui->comboBox->setModel(modal);

qry->next();
qDebug() << (modal->rowCount());

}


A coupe of things for this:
- no need to create the model every time, just create it once and keep it in a member variable.
- you then only need to set it once one the table as well
- Don't create the query on the heap, you are leaking it
- in fact you can just pass the SQL string to the model
- see if you have the data you expect inside the model, e.g


qDebug() << model->index(0, 0).data(Qt::DisplayRole);

or create a QTableView (without parent it becomes a separte window) and let it have the same model

Cheers,
_

diyanafadzil
17th September 2015, 15:03
Hello anda_skoa! thank you for your great solution..
Somehow I dont quite understand it. Can you give me example on how to prevent the query from leaking? :(
I edit my code so far but it did not work. :(

void FormDialog::on_loadData_clicked()
{
connOpen();
QSqlQueryModel * modal = new QSqlQueryModel(ui->comboBox);
QSqlQuery qry;
qry.prepare("SELECT FaceID FROM templateinfo");
qry.exec();
modal->setQuery(qry);


Added after 9 minutes:

Hello anda_skoa! thank you for your great solution..
Somehow I dont quite understand it. Can you give me example on how to prevent the query from leaking?
I edit my code so far but it did not work.

void FormDialog::on_loadData_clicked()
{
connOpen();
QSqlQueryModel * modal = new QSqlQueryModel(ui->comboBox);
QSqlQuery qry;
qry.prepare("SELECT FaceID FROM templateinfo");
qry.exec();
modal->setQuery(qry);

anda_skoa
17th September 2015, 15:59
Yes, not calling "new" but having it on the heap is the correct way.
In this case, as pointed out, you could just directly pass the SQL string to modal->setQuery(), no need for a QSqlQuery object at all.

You will of course still leak the model if you click that button a second time.
Easily avoidable by not using a local variable but a member and only creating it once, e.g. right in the constructor.

Cheers,
_