Results 1 to 4 of 4

Thread: MySql data not shown in QcomboBox

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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,
    _

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

    diyanafadzil (17th September 2015)

Similar Threads

  1. Replies: 15
    Last Post: 14th September 2015, 13:42
  2. How to get data from mysql ?
    By BuranereLoo in forum Newbie
    Replies: 3
    Last Post: 3rd September 2015, 13:23
  3. Replies: 4
    Last Post: 5th March 2010, 14:20
  4. get mysql table into QComboBox
    By eleanor in forum Qt Programming
    Replies: 17
    Last Post: 10th October 2007, 15:35
  5. Replies: 2
    Last Post: 6th April 2006, 08: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
  •  
Qt is a trademark of The Qt Company.