Results 1 to 4 of 4

Thread: MySql data not shown in QcomboBox

  1. #1
    Join Date
    Sep 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    qt.png

    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.

    qt2.png

    This is my code :

    Qt Code:
    1. void FormDialog::on_comboBox_currentIndexChanged(const QString &arg1)
    2. {
    3.  
    4. connOpen();
    5.  
    6. Face = ui->comboBox->currentText();
    7.  
    8. QSqlQuery qry;
    9.  
    10. qry.prepare("SELECT * FROM templateinfo WHERE FaceID='"+Face+"'");
    11.  
    12. if(qry.exec())
    13. {
    14. while(qry.next())
    15. {
    16. ui ->FaceIDLineEdit->setText(qry.value(0).toString());
    17. ui ->nameLineEdit->setText(qry.value(1).toString());
    18. ui ->female->setChecked(qry.value(2).toBool());
    19. ui ->male->setChecked(qry.value(3).toBool());
    20. ui ->WantedlineEdit->setText(qry.value(4).toString());
    21. qDebug() << "execute";
    22. }
    23.  
    24. connClose();
    25. }
    26.  
    27. else {
    28. qDebug() << "not execute";
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void FormDialog::on_loadData_clicked()
    2. {
    3. connOpen();
    4. QSqlQueryModel * modal = new QSqlQueryModel();
    5. QSqlQuery* qry = new QSqlQuery(db);
    6. qry->prepare("SELECT FaceID FROM templateinfo");
    7. qry->exec();
    8. modal->setQuery(*qry);
    9.  
    10. ui->comboBox->setModel(modal);
    11.  
    12. qry->next();
    13. qDebug() << (modal->rowCount());
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    Please help me solve this problem as I am new in Qt and I dont know which part is wrong
    Last edited by diyanafadzil; 17th September 2015 at 08:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySql data not shown in QcomboBox

    Quote Originally Posted by diyanafadzil View Post
    Qt Code:
    1. qry.prepare("SELECT * FROM templateinfo WHERE FaceID='"+Face+"'");
    To copy to clipboard, switch view to plain text mode 
    Don't concatenate values onto an QSL query, use QSqlQuery::bindValue() instead.

    Quote Originally Posted by diyanafadzil View Post
    Qt Code:
    1. void FormDialog::on_loadData_clicked()
    2. {
    3. connOpen();
    4. QSqlQueryModel * modal = new QSqlQueryModel();
    5. QSqlQuery* qry = new QSqlQuery(db);
    6. qry->prepare("SELECT FaceID FROM templateinfo");
    7. qry->exec();
    8. modal->setQuery(*qry);
    9.  
    10. ui->comboBox->setModel(modal);
    11.  
    12. qry->next();
    13. qDebug() << (modal->rowCount());
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. qDebug() << model->index(0, 0).data(Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    or create a QTableView (without parent it becomes a separte window) and let it have the same model

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    diyanafadzil (17th September 2015)

  4. #3
    Join Date
    Sep 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    Qt Code:
    1. void FormDialog::on_loadData_clicked()
    2. {
    3. connOpen();
    4. QSqlQueryModel * modal = new QSqlQueryModel(ui->comboBox);
    5. QSqlQuery qry;
    6. qry.prepare("SELECT FaceID FROM templateinfo");
    7. qry.exec();
    8. modal->setQuery(qry);
    To copy to clipboard, switch view to plain text mode 


    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.
    Qt Code:
    1. void FormDialog::on_loadData_clicked()
    2. {
    3. connOpen();
    4. QSqlQueryModel * modal = new QSqlQueryModel(ui->comboBox);
    5. QSqlQuery qry;
    6. qry.prepare("SELECT FaceID FROM templateinfo");
    7. qry.exec();
    8. modal->setQuery(qry);
    To copy to clipboard, switch view to plain text mode 
    Last edited by diyanafadzil; 17th September 2015 at 16:03.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. Replies: 15
    Last Post: 14th September 2015, 14:42
  2. How to get data from mysql ?
    By BuranereLoo in forum Newbie
    Replies: 3
    Last Post: 3rd September 2015, 14:23
  3. Replies: 4
    Last Post: 5th March 2010, 15:20
  4. get mysql table into QComboBox
    By eleanor in forum Qt Programming
    Replies: 17
    Last Post: 10th October 2007, 16:35
  5. Replies: 2
    Last Post: 6th April 2006, 09:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.