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. How to get field value from QStandardItemModel using header name and row number

How to get field value from QStandardItemModel using header name and row number

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 11.9k 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.
  • A Offline
    A Offline
    ashokb
    wrote on last edited by
    #1

    Hi,
    I have a QTableView for which i am using QStandardItemModel. I have around 20 columns in this model.
    I want to get data of any specific field using row number and column header name.
    Write now i am getting values by giving row number and column number in this way,
    @
    QModelIndex index = finalResultTable->model()->index(20, 0);
    QString relativePath = finalResultTable->model()->data(index).toString();
    @

    Problem with this is if i change column sequence in my database(mysql) i need to change all column numbers in code. There must be solution for this but i unable to find out. Any help will be appreciated.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hareen Laks
      wrote on last edited by
      #2

      ashokb,

      You can create table as follows..

      @QTableView *table_view;
      QStandardItemModel *item_model;

      void createTable()
      {
      table_view = new QTableView();
      item_model = new QStandardItemModel(0,0,this);
      table_view->setModel(item_model);

       item_model->clear();
      

      //setOfRows() retrives QStringList which contains row wise header names.
      //setOfColomns() retrives QStringList which contains colomn wise header names.

       item_model->setVerticalHeaderLabels(setOfRows());
       item_model->setHorizontalHeaderLabels(setOfColomns());
      
       for (int row = 0; row < setOfRows().count(); ++row)
          {
              for (int col = 0; col < setOfColomns().count(); ++col)
              {
                  QModelIndex index = item_model->index(row,col,QModelIndex());
      
                   QStandardItem *item = item_model->itemFromIndex(index);
                   item_model->setData(index,0);
              }
          }
      }@
      

      And retrieve data as follows.

      @void findData()
      {
      QString row_name;
      QString col_name;
      QString relative_path;

          for (int row = 0; row < setOfRows().count(); ++row)
          {
              for (int col = 0; col < setOfColomns().count(); ++col)
              {
                  QModelIndex index = item_model->index(row,col,QModelIndex());
                  relative_path = item_model->data(index).toString();
      
                       row_name = setOfRows().at(row);
                       col_name = setOfColomns().at(col);
               }
          }
      }@
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I don't get the question. You say you are using a QStandardItemModel. So far so good. Then, all of a sudden, a database comes into the picture. How is that database hooked up? What is the relationship with your QStandardItemModel? Is there a specific reason you are not using QSqlQueryModel or QSqlTableModel when you are using a database underlying an item view?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hareen Laks
          wrote on last edited by
          #4

          [quote author="Andre" date="1399444049"]I don't get the question. You say you are using a QStandardItemModel. So far so good. Then, all of a sudden, a database comes into the picture. How is that database hooked up? [/quote]

          I also confused. But I assumed he got colomn header names and raw header names from DB.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ashokb
            wrote on last edited by
            #5

            @Andre
            Hi, Sorry for confusing question.

            1. I have a table in my database.
            2. I fetch few columns and one row at a time according to inputs from user on gui.
            3. Then i construct a QStandardItemModel using QStandardItem for that fetched data. i don't want to select all data from my database table in model, and (as per my knowledge) QSqlTableModel fetch all data in model. (I don't know whether it is possible to fetch data in QSqlTableModel using something like 'WHERE' in mysql).
            4. Then i set that model(which contains only one row) in table view. Then user edit some fields, some get edited by programmatically(calculations) and when user press save i take data from each field of model, build a query and insert or update that in database.

            Now, i want to fetch or set field data from my model by giving column header name. If there is better and efficient way of doing this please let me know, i will implement that, after all i want my software efficient. If i still not clear fill free to ask. (I am little weak in English as well).

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

              By all means: please study using QSqlQueryModel and QSqlTableModel. Using the latter (with the filter it provides), you can have your updatable view done in few lines of code. QSqlQueryModel is not updateble by default.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ashokb
                wrote on last edited by
                #7

                @Andre,
                Hi,
                Thanks for you advice, i will definitely study both QSqlQueryModel and QSqlTableModel. Thanks...

                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