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. QSqlTableModel column to QStringList
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel column to QStringList

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.8k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    betmens
    wrote on last edited by
    #1

    I have QSqlTableModel:
    id|name|data
    0|name1|data1
    1|name2|data2
    2|name2|data
    How I can get QstringList from column name with result: ("name1", "name2", "name3")

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      @
      QStringList nameList;

      // make sure the complete result set is fetched
      while (myModel->canFetchMore())
      myModel->fetchMore();

      for (int r = 0; r < model->rowCount(); ++r) {
      nameList << model->data(model->index(r,1));
      }
      @

      [Brain to terminal, not tested]

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • B Offline
        B Offline
        betmens
        wrote on last edited by
        #3

        Thanks. I wanted simplest, but with little function works good.
        @QStringList get_str_col(QSqlTableModel *tm, QString col_name){
        int i, rc = tm->rowCount();
        QStringList list;
        for (i = 0; i < rc; i++) list << tm->data(tm->index(i, tm->fieldIndex(col_name))).toString();
        return list;
        }
        QSqlTableModel *myModel;
        QStringList column_list;
        column_list = get_str_col(myModel, "name");@

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Some remarks:

          • save the index of the field in a variable. It will speed up your method, as you do not need to look up the index for each row (it doesn't change!)
          • rowCount() is not guaranteed to return the number of rows in the result set. If the database driver does not support returning the number of rows in a query (which is the case for Oracle, for example), you do get only the number of rows already fetched. In that case you are likely to miss rows

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • B Offline
            B Offline
            betmens
            wrote on last edited by
            #5

            About index I agree. And in Your example "model->rowCount()" should also be declare. At least in php works faster. Tested myself.
            rowCount() fetch probably be useful in future.
            How You think is here a difference between ++r vs r++?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              What do you gain, if you method is fast, but wrong? Nothing than pain in the future!

              Premature optimization is the cause of many evil! Make your method work correctly first, and then do optimizations and only if you run into performance issues!

              ++r or r++ is negligible in that case. Ask Google for the myriads of performance discussions on this topic please.

              http://www.catb.org/~esr/faqs/smart-questions.html

              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