[SOLVED] About setting a QSqlTableModel to a QStringList



  • If I want a column of values from the database to appear in a QComboBox, I use the model/view programming to achieve it with the code given below

    @
    QSqlTableModel m = new QSqlTableModel();
    m->setTable("table");
    m->setModelColumn(3);
    m->select();

    QComboBox *comboBox = new QCombobox();
    comboBox->setModel(m);
    @

    I don't want a comboBox but I simply want to store the values in a QStringList and loop through each value to do something with it. My questions are:

    1. Is it possible with a QStringList? Because I'm not able to set the model it as

    @
    QStringList stringList;
    stringList.setModel();
    @

    1. If not QStringList, is it ok to use a QComboBox itself? If not, what are the other options?


  • You can do that:
    @
    QSqlTableModel m = new QSqlTableModel();
    m->setTable("table");
    m->setModelColumn(3);
    m->select();

     QStringList stringList;
       for(int i = 0; i < m->rowCount(); i++)
        stringList.append( m->record(i).value(your_index_column).toString());
    

    @

    And your values is charged in stringList



  • never mind



  • I'd just skip using models at all, and use [[doc:QSqlQuery]] directly:

    @
    QSqlQuery query = QSqlQuery(db); //db is your database object
    query.exec("SELECT MyColumnName FROM MyTableName"); //optionally add filtering or ordering using WHERE and ORDER BY clauses
    QStringList results;
    while(query.next()) {
    results << query.value(0).toString();
    }
    @
    The model/view architecture is there for putting data in views in a generic way. It is not all that suitable for other purposes. Other APIs are more suitable for that. If you find yourself directly manipulating QAbstractItemModel API while you're not building your own models and views, you are probably doing it wrong(TM).



  • have you tested my code?



  • have you tested my code?
    [quote author="qxoz" date="1359542026"]never mind[/quote]



  • Your code should work:
    @QSqlTableModel *m = new QSqlTableModel();
    m->setTable("table");
    m->select();

     QStringList stringList;
       for(int i = 0; i < m->rowCount(); i++)
        stringList.append( m->record(i).value(your_index_column).toString());@
    

    but Andre shows more optimal variant



  • Thanks for the reply guys. Why I said I'm using a model is because I have been asked to use a model instead of direct SQL queries. I will however show both versions i.e. maxoreli and Andre's code, to my mentor and see which one I can use. I'll get back to you guys and tell you which one I finally used.

    Thanks a lot!



  • Hi again!

     Just an update to let y'all know that I went with Andre's solution. Using models became very complicated and resulted in many many lines of code. So yeah, I used QSqlQuery directly. 
    

    On the other hand, when I tried maxoreli's solution, I had

    @
    model = new QSqlQueryModel();
    model->setQuery(s);
    label->setText(model->record(0).toString();
    @

    and this gives me an error:

    1. Use of undefined type QSqlRecord
    2. Left of .toString must point to class/struct/union/generic type

    Why?



  • Add #include<QSqlRecord> on top of your source



  • That did it! Thanks. I included QSqlQueryModel and was wondering why it isn't working. Thanks a lot.



  • If still not works do following steps:

    1. Build -> Clean All
    2. Build -> Run qmake
    3. Build -> Rebuild All


  • Thank you. I had to edit the previous post cuz I thought you said include QSqlQueryModel and not QSqlRecord



  • EDIT: Please ignore the following comment. There was a problem with my database and not my code.

    Ok guys, I followed Andre's method and I'm not getting any errors. However, I'm not getting any output either. Here's the code

    @
    QSqlQuery query = QSqlQuery(db); //db is the database object
    QString st = QString("SELECT TIME FROM SCHEDULE WHERE ID = %1").arg(str);
    query.prepare(st);
    query.exec();

    QStringList strlist;

    while(query.next())
    {
    strlist.append(query.value(0).toString());
    qDebug()<<query.value(0).toString();
    }
    @

    The qDebug does not display anything. Where am I going wrong? Please help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.