Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    treeview file save
    2
    5
    3817
    Loading More Posts
    • 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
      fbengo last edited by fbengo

      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
      
      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @fbengo last edited by raven-worx

        @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 Reply Quote 2
        • F
          fbengo last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • F
            fbengo @raven-worx last edited by fbengo

            @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

            raven-worx 1 Reply Last reply Reply Quote 0
            • raven-worx
              raven-worx Moderators @fbengo last edited by

              @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 Reply Quote 0
              • First post
                Last post