2 Attachment(s)
MySql data not shown in QcomboBox
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:
Attachment 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.
Attachment 11378
This is my code :
Code:
void FormDialog
::on_comboBox_currentIndexChanged(const QString &arg1
) {
connOpen();
Face = ui->comboBox->currentText();
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";
}
}
Code:
void FormDialog::on_loadData_clicked()
{
connOpen();
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 :(
Re: MySql data not shown in QcomboBox
Quote:
Originally Posted by
diyanafadzil
Code:
qry.prepare("SELECT * FROM templateinfo WHERE FaceID='"+Face+"'");
Don't concatenate values onto an QSL query, use QSqlQuery::bindValue() instead.
Quote:
Originally Posted by
diyanafadzil
Code:
void FormDialog::on_loadData_clicked()
{
connOpen();
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
Code:
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,
_
Re: MySql data not shown in QcomboBox
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. :(
Code:
void FormDialog::on_loadData_clicked()
{
connOpen();
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.
Code:
void FormDialog::on_loadData_clicked()
{
connOpen();
qry.prepare("SELECT FaceID FROM templateinfo");
qry.exec();
modal->setQuery(qry);
Re: MySql data not shown in QcomboBox
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,
_