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] About setting a QSqlTableModel to a QStringList

[SOLVED] About setting a QSqlTableModel to a QStringList

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 5.2k 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

    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?
    1 Reply Last reply
    0
    • M Offline
      M Offline
      maxoreli
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        never mind

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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).

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxoreli
            wrote on last edited by
            #5

            have you tested my code?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxoreli
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #7

                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

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

                  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!

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

                    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?

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #10

                      Add #include<QSqlRecord> on top of your source

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

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

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on last edited by
                          #12

                          If still not works do following steps:

                          1. Build -> Clean All
                          2. Build -> Run qmake
                          3. Build -> Rebuild All
                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            holygirl
                            wrote on last edited by
                            #13

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

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

                              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.

                              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