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. I have a QStandardItemModel and I need to iterate through it so I can write a custom format line to text file. Can't figure it out.
Forum Updated to NodeBB v4.3 + New Features

I have a QStandardItemModel and I need to iterate through it so I can write a custom format line to text file. Can't figure it out.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.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.
  • R Offline
    R Offline
    RobbieP
    wrote on last edited by
    #1

    I have a QStandardItemModel that's been sorted into a QSortFilterProxyModel. I need to iterate through all of it's records and pull the data and create a text line that I can append to a file.

    Here's my model:

    QStandardItemModel *model = new QStandardItemModel(0, 3, parent);
    
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Data"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Source"));
    

    Here's some code I found in another answer elsewhere but it doesn't work:

    // DO FILE WRITES
    int n = proxyModel->sourceModel()->rowCount();
    int m = proxyModel->sourceModel()->columnCount();
    for (int i=0; i<n; ++i)
    {
        for (int j=0; j<m; j++)
        {
            QMap thisItem = proxyModel->sourceModel()->itemData(proxyModel->sourceModel()->index(i,j));
            QString thisLine;
            thisLine = thisLine + thisItem + ",";
            textOut << thisLine;
        }
    }
    textOut << endl;
    

    Anyone have any ideas?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RobbieP
      wrote on last edited by
      #2

      I figured it out:

      // DO FILE WRITES
      int n = proxyModel->sourceModel()->rowCount();
      int m = proxyModel->sourceModel()->columnCount();
      for (int i=0; i<n; ++i)
      {
          QString thisName = proxyModel->sourceModel()->data(proxyModel->sourceModel()->index(i, 0)).toString();
          QString thisValue = proxyModel->sourceModel()->data(proxyModel->sourceModel()->index(i, 1)).toString();
          QString thisSource = proxyModel->sourceModel()->data(proxyModel->sourceModel()->index(i, 2)).toString();
          QString thisLine;
          thisLine = thisLine + thisName + "," + thisValue + "," + thisSource + ",";
          textOut << thisLine;
      }
      textOut << endl;
      
      JonBJ 1 Reply Last reply
      0
      • R RobbieP

        I figured it out:

        // DO FILE WRITES
        int n = proxyModel->sourceModel()->rowCount();
        int m = proxyModel->sourceModel()->columnCount();
        for (int i=0; i<n; ++i)
        {
            QString thisName = proxyModel->sourceModel()->data(proxyModel->sourceModel()->index(i, 0)).toString();
            QString thisValue = proxyModel->sourceModel()->data(proxyModel->sourceModel()->index(i, 1)).toString();
            QString thisSource = proxyModel->sourceModel()->data(proxyModel->sourceModel()->index(i, 2)).toString();
            QString thisLine;
            thisLine = thisLine + thisName + "," + thisValue + "," + thisSource + ",";
            textOut << thisLine;
        }
        textOut << endl;
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @RobbieP
        This is fine, but you should not have to do everything so long-windedly through proxyModel->sourceModel() calls. Did you try e.g.

        int n = proxyModel->rowCount();
        ...
        QString thisName = proxyModel->index(i, 0).data().toString();
        

        Also note this outputs in the "sorted into a QSortFilterProxyModel" order, your original outputs in the underlying, unsorted QStandardItemModel order. Depends which you want.

        1 Reply Last reply
        1

        • Login

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