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 save a treeview as .csv file in Qt?
QtWS25 Last Chance

How to save a treeview as .csv file in Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
treeviewfilesave
5 Posts 2 Posters 4.6k 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.
  • F Offline
    F Offline
    fbengo
    wrote on 25 Jan 2016, 14:43 last edited by fbengo
    #1

    I need to save the current view of my QTreeView (QSortFilterProxyModel and QStandardItemModel) as a .csv file. Unfortunately, I don't know where to start...

    My code only prints the parent (root) items. I need to print both parents and their children.
    I think this can be solved by recursion, but I am not sure how... Any ideas?

    My treeview:

    http://i.imgur.com/R9rvPDh.png

    My code:

    QFile file(filePath);
                if (file.open(QFile::WriteOnly))
                {
                    QTextStream stream(&file);              
                    for (int i = 0; i < proxyModel->rowCount(); i++)
                    {
                        QModelIndex index0 = proxyModel->index(i, 0);
                        QModelIndex index1 = proxyModel->index(i, 1);
                        QModelIndex index2 = proxyModel->index(i, 2);
                        QModelIndex index3 = proxyModel->index(i, 3);
                        QModelIndex index4 = proxyModel->index(i, 4);
                        stream << proxyModel->data(index0).toString() << "," <<  proxyModel->data(index1).toString() << "," << proxyModel->data(index2).toString() << "," << proxyModel->data(index3).toString() << "," << proxyModel->data(index4).toString();
                        stream << "\n";                 
                    }
                    file.close();
                }
    

    Ouput:

    (0008,0005),SpecificCharacterSet,CS,10,ISO_IR 100
    (0008,0008),ImageType,CS,36,ORIGINAL\PRIMARY\M\HEADER_CORRECTED
    (0008,0016),SOPClassUID,UI,26,1.2.840.10008.5.1.4.1.1.4
    (0008,0018),SOPInstanceUID,UI,46,1.3.12.2.1107.5.2.5.11090.5.0.5823661031981777
    (0008,0020),StudyDate,DA,8,20040305
    (0008,0021),SeriesDate,DA,8,20040305
    (0008,0022),AcquisitionDate,DA,8,20040305
    (0008,0023),ContentDate,DA,8,20040305
    (0008,0030),StudyTime,TM,14,085922.859000
    (0008,0031),SeriesTime,TM,14,090019.359000
    (0008,0032),AcquisitionTime,TM,14,085939.762492
    (0008,0033),ContentTime,TM,14,090021.062000
    (0008,0050),AccessionNumber,SH,2,0
    (0008,0060),Modality,CS,2,MR
    (0008,0070),Manufacturer,LO,8,SIEMENS
    (0008,0080),InstitutionName,LO,18,cJf7JCqV84P^te1az
    (0008,0090),ReferringPhysicianName,PN,20,FLp8xklEDWOqavQWiJ9
    (0008,1010),StationName,SH,8,unknown
    (0008,1030),StudyDescription,LO,12,WRIST^RIGHT
    (0008,103e),SeriesDescription,LO,18,SCOUT 3-PLANE RT.
    (0008,1070),OperatorsName,PN,14,RIORDAN, JAMES
    
    R 1 Reply Last reply 25 Jan 2016, 18:32
    0
    • F fbengo
      25 Jan 2016, 14:43

      I need to save the current view of my QTreeView (QSortFilterProxyModel and QStandardItemModel) as a .csv file. Unfortunately, I don't know where to start...

      My code only prints the parent (root) items. I need to print both parents and their children.
      I think this can be solved by recursion, but I am not sure how... Any ideas?

      My treeview:

      http://i.imgur.com/R9rvPDh.png

      My code:

      QFile file(filePath);
                  if (file.open(QFile::WriteOnly))
                  {
                      QTextStream stream(&file);              
                      for (int i = 0; i < proxyModel->rowCount(); i++)
                      {
                          QModelIndex index0 = proxyModel->index(i, 0);
                          QModelIndex index1 = proxyModel->index(i, 1);
                          QModelIndex index2 = proxyModel->index(i, 2);
                          QModelIndex index3 = proxyModel->index(i, 3);
                          QModelIndex index4 = proxyModel->index(i, 4);
                          stream << proxyModel->data(index0).toString() << "," <<  proxyModel->data(index1).toString() << "," << proxyModel->data(index2).toString() << "," << proxyModel->data(index3).toString() << "," << proxyModel->data(index4).toString();
                          stream << "\n";                 
                      }
                      file.close();
                  }
      

      Ouput:

      (0008,0005),SpecificCharacterSet,CS,10,ISO_IR 100
      (0008,0008),ImageType,CS,36,ORIGINAL\PRIMARY\M\HEADER_CORRECTED
      (0008,0016),SOPClassUID,UI,26,1.2.840.10008.5.1.4.1.1.4
      (0008,0018),SOPInstanceUID,UI,46,1.3.12.2.1107.5.2.5.11090.5.0.5823661031981777
      (0008,0020),StudyDate,DA,8,20040305
      (0008,0021),SeriesDate,DA,8,20040305
      (0008,0022),AcquisitionDate,DA,8,20040305
      (0008,0023),ContentDate,DA,8,20040305
      (0008,0030),StudyTime,TM,14,085922.859000
      (0008,0031),SeriesTime,TM,14,090019.359000
      (0008,0032),AcquisitionTime,TM,14,085939.762492
      (0008,0033),ContentTime,TM,14,090021.062000
      (0008,0050),AccessionNumber,SH,2,0
      (0008,0060),Modality,CS,2,MR
      (0008,0070),Manufacturer,LO,8,SIEMENS
      (0008,0080),InstitutionName,LO,18,cJf7JCqV84P^te1az
      (0008,0090),ReferringPhysicianName,PN,20,FLp8xklEDWOqavQWiJ9
      (0008,1010),StationName,SH,8,unknown
      (0008,1030),StudyDescription,LO,12,WRIST^RIGHT
      (0008,103e),SeriesDescription,LO,18,SCOUT 3-PLANE RT.
      (0008,1070),OperatorsName,PN,14,RIORDAN, JAMES
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 25 Jan 2016, 18:32 last edited by raven-worx
      #2

      @fbengo

      void someFunction()
      {
              QFile file(filePath);
                  if (file.open(QFile::WriteOnly))
                  {
                      QTextStream stream(&file);
                      this->printTree( 0, QModelIndex(), stream );
                      file.close();
                  }
      }
      
      void printTree( int level, const QModelIndex & index, QTextStream  & stream )
      {
               QString indent;
               for( int j = 0; j < level; ++j )
                   indent.append( "   " );
      
              // print index itself
              if( index.isValid() )
              {
                   stream << indent;
                   for( int c = 0; c < index.model()->columnCount(index.parent()); ++c )
                   {
                           QModelIndex columnIndex = index.sibling(index.row(), c);
                           stream << index.data().toString();
                   }
                   stream << "\\n";
             }
      
             //print children
             for (int r = 0; r < index.model()->rowCount(index); r++)
              {
                     const QModelIndex childIndex = index.child( r, 0 );
                      this->printTree( level+1, childIndex, stream );
              }
      }
      

      No guarantee that it works - wrote it straight down from my head - but i hope it gives you an idea, how the recursion can be done.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      F 1 Reply Last reply 26 Jan 2016, 19:23
      2
      • F Offline
        F Offline
        fbengo
        wrote on 26 Jan 2016, 19:15 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • R raven-worx
          25 Jan 2016, 18:32

          @fbengo

          void someFunction()
          {
                  QFile file(filePath);
                      if (file.open(QFile::WriteOnly))
                      {
                          QTextStream stream(&file);
                          this->printTree( 0, QModelIndex(), stream );
                          file.close();
                      }
          }
          
          void printTree( int level, const QModelIndex & index, QTextStream  & stream )
          {
                   QString indent;
                   for( int j = 0; j < level; ++j )
                       indent.append( "   " );
          
                  // print index itself
                  if( index.isValid() )
                  {
                       stream << indent;
                       for( int c = 0; c < index.model()->columnCount(index.parent()); ++c )
                       {
                               QModelIndex columnIndex = index.sibling(index.row(), c);
                               stream << index.data().toString();
                       }
                       stream << "\\n";
                 }
          
                 //print children
                 for (int r = 0; r < index.model()->rowCount(index); r++)
                  {
                         const QModelIndex childIndex = index.child( r, 0 );
                          this->printTree( level+1, childIndex, stream );
                  }
          }
          

          No guarantee that it works - wrote it straight down from my head - but i hope it gives you an idea, how the recursion can be done.

          F Offline
          F Offline
          fbengo
          wrote on 26 Jan 2016, 19:23 last edited by fbengo
          #4

          @raven-worx

          Thank you very much for your effort, but I have still a question...
          How do I use this on my proxyModel?

          NEVERMIND, I FOUND THE MISTAKE :)

           for( int c = 0; c < index.model()->columnCount(index.parent()); c++)
                       {
                               QModelIndex columnIndex = index.sibling(index.row(), c);
                               stream << index.data().toString();
                       }
          

          SHOULD BE:

           for( int c = 0; c < index.model()->columnCount(index.parent()); c++)
                       {
                               QModelIndex columnIndex = index.sibling(index.row(), c);
                               stream << columnIndex .data().toString();
                       }
          

          THANK YOU SOOOO MUCH FOR YOUR HELP!! I cannot believe how you can get up with this, without trying :) I am new in the Qt world and one day I wil also be experienced as you.. :P

          R 1 Reply Last reply 26 Jan 2016, 19:59
          0
          • F fbengo
            26 Jan 2016, 19:23

            @raven-worx

            Thank you very much for your effort, but I have still a question...
            How do I use this on my proxyModel?

            NEVERMIND, I FOUND THE MISTAKE :)

             for( int c = 0; c < index.model()->columnCount(index.parent()); c++)
                         {
                                 QModelIndex columnIndex = index.sibling(index.row(), c);
                                 stream << index.data().toString();
                         }
            

            SHOULD BE:

             for( int c = 0; c < index.model()->columnCount(index.parent()); c++)
                         {
                                 QModelIndex columnIndex = index.sibling(index.row(), c);
                                 stream << columnIndex .data().toString();
                         }
            

            THANK YOU SOOOO MUCH FOR YOUR HELP!! I cannot believe how you can get up with this, without trying :) I am new in the Qt world and one day I wil also be experienced as you.. :P

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 26 Jan 2016, 19:59 last edited by
            #5

            @fbengo
            oops..sry my bad.
            but as i said... straight from my head :)

            just adapt the starting method:

            void someFunction()
            {
                    QFile file(filePath);
                        if (file.open(QFile::WriteOnly))
                        {
                            QTextStream stream(&file);
                            for (int r = 0; r < model->rowCount(); r++)
                                    this->printTree( 0, model->index(r,0), stream );
                            file.close();
                        }
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            3/5

            26 Jan 2016, 19:15

            • Login

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