[SOLVED] Help with iterating through the fields in SQLite



  • Hi everybody!

        I'm trying to write an application where a label on the UI displays one field from a particular column of a table in the database. I have two buttons _up_ and _down_ which when pressed will display the next field and the previous field of the column respectively. 
    

    I'm sorry but I don't have much code to get me going. I don't know where to begin. A QStringList perhaps?
    Please do help.
    Thanks.

    @
    m = new QSqlTableModel();
    m->setTable("tablename");
    m->select();
    m->removeColumns(1,4);
    @

    This is how I get the required column. Now I need to put it into a QStringList or something so I can iterate through it. But I'm not finding much help on Google to achieve what I wish to. Help!

    EDIT: I came up with a solution but I'm not sure if this is the right way. Can somebody please tell me if it is?

    @
    QComboBox *cb = new QComboBox();
    cb->setModel(m);
    label->setText(cb->currentText());
    if(button_up->clicked())
    {
    cb->setCurrentIndex(cb->currentIndex()+1);
    label->setText(cb->currentText());
    }
    else
    {
    cb->setCurrentIndex(cb->currentIndex()-1);
    label->setText(cb->currentText());
    }
    @



  • using models to retrieve and fetch data is the most common solution in Qt while models are index based and they are fast however there's no feature to bind columns into objects and you should do it yourself.
    for this manner you may either consider create a class to handle your iteration and values to objects or go simpler like what you did.



  • I just wanted to know if my approach is acceptable or if it is "bad" coding. Anyway, thanks for clearing it for me. I guess I'll stick to this method then.


Log in to reply
 

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