Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Help with iterating through the fields in SQLite
QtWS25 Last Chance

[SOLVED] Help with iterating through the fields in SQLite

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #1

    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());
    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        holygirl
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved