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. qtreeview structure in a tex file
Forum Updated to NodeBB v4.3 + New Features

qtreeview structure in a tex file

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.8k Views 2 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.
  • S Offline
    S Offline
    saber
    wrote on last edited by VRonin
    #1

    i use qtreeview to create the file system .
    is there any to get the view in a text file .
    like this

    home
        | Desktop
        | Downloads
        |   |Picture
        |   |Audio
        |   |   |a.mp3
        |   |   |m.mp3
        | Picture
    

    like this.
    any way to get like this in a text??

    VRoninV 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      You'll have to iterate through it one child item at a time and build the text yourself.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        saber
        wrote on last edited by
        #3

        my friend help me to archive this.here is the code.
        it takes a while to make the text .

        void getDirText(const QString &path)
        {
            // Check whether hidden file or folder collected or not
            bool isHidden = 0;
        
            // Store the directories
            QStringList dirs;
            // Store the files
            QStringList files;
        
            // Set the filltering modes
            QDir::Filters filters = QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot | QDir::NoSymLinks;
        
            // Get hidden file or folder if true
            if (isHidden)
                filters |= QDir::Hidden;
        
        
            QDirIterator it(path, filters);
        
            while (it.hasNext()) {
                it.next();
        
                if(it.fileInfo().isDir()) {
                    // Skip the given path
                    if (it.fileInfo().completeBaseName() == QFileInfo(path).completeBaseName())
                        continue;
        
                    dirs.append(it.fileInfo().fileName());
                } else {
                    files.append(it.fileInfo().fileName());
                }
            }
        
            // Make a natural sort for both file and dir lists
            QCollator col;
            col.setNumericMode(true);
        
            // Sort for directories
            std::sort(dirs.begin(), dirs.end(), col);
        
            // Sort for files
            std::sort(files.begin(), files.end(), col);
        
            // Store the sorted data to a string for saving it to file
            QString output;
        
            foreach (QString s, dirs) {
                output.append(s + "\n");
            }
        
            foreach (QString s, files) {
                output.append(s + "\n");
            }
        
            // Save path for the collected file folder list
            QString filePath = path + "/" + QFileInfo(path).baseName() + ".txt";
        
            QFile file(filePath);
            file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
            QTextStream text(&file);
            text << output;
            file.close();
        }
        
        

        thanks

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          QString treeToText(const QAbstractItemModel* model, const QModelIndex& parent=QModelIndex(), const QString& head = QString()){
          QString result;
          for(int i=0, maxRow = model->rowCount(parent);i<maxRow;++i){
          const QModelIndex currIdx = model->index(i,0,parent);
          result += head + currIdx.data().toString() + '\n';
          if(model->hasChildren(currIdx))
          result += treeToText(model,currIdx,head + "    |");
          }
          result.chop(1);
          return result;
          }
          

          then you can just QTextStream(textFile) << treeToText(myModel);

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          2
          • VRoninV VRonin
            QString treeToText(const QAbstractItemModel* model, const QModelIndex& parent=QModelIndex(), const QString& head = QString()){
            QString result;
            for(int i=0, maxRow = model->rowCount(parent);i<maxRow;++i){
            const QModelIndex currIdx = model->index(i,0,parent);
            result += head + currIdx.data().toString() + '\n';
            if(model->hasChildren(currIdx))
            result += treeToText(model,currIdx,head + "    |");
            }
            result.chop(1);
            return result;
            }
            

            then you can just QTextStream(textFile) << treeToText(myModel);

            S Offline
            S Offline
            saber
            wrote on last edited by saber
            #5

            @VRonin said in qtreeview structure in a tex file:

            treeToText

            i am getting this error
            0_1534136680891_w.png

            and did not understand how to use it

            jsulmJ 1 Reply Last reply
            0
            • S saber

              @VRonin said in qtreeview structure in a tex file:

              treeToText

              i am getting this error
              0_1534136680891_w.png

              and did not understand how to use it

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @saber Well, the error message says what is wrong. Convert QVariant to string.
              Can you please stop posting screen-shots? It makes hard to copy your code and make changes...

              result = head + currIdx.data().toString() + '\n';
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              3
              • jsulmJ jsulm

                @saber Well, the error message says what is wrong. Convert QVariant to string.
                Can you please stop posting screen-shots? It makes hard to copy your code and make changes...

                result = head + currIdx.data().toString() + '\n';
                
                S Offline
                S Offline
                saber
                wrote on last edited by
                #7

                @jsulm ok .it solved the error.
                but can't understand out how to use that if only i have a folder path?

                jsulmJ 1 Reply Last reply
                0
                • S saber

                  @jsulm ok .it solved the error.
                  but can't understand out how to use that if only i have a folder path?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @saber said in qtreeview structure in a tex file:

                  but can't understand out how to use that if only i have a folder path?

                  How to use what?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @saber said in qtreeview structure in a tex file:

                    but can't understand out how to use that if only i have a folder path?

                    How to use what?

                    S Offline
                    S Offline
                    saber
                    wrote on last edited by
                    #9

                    @jsulm use the function that @VRonin gives

                    1 Reply Last reply
                    0
                    • S saber

                      i use qtreeview to create the file system .
                      is there any to get the view in a text file .
                      like this

                      home
                          | Desktop
                          | Downloads
                          |   |Picture
                          |   |Audio
                          |   |   |a.mp3
                          |   |   |m.mp3
                          | Picture
                      

                      like this.
                      any way to get like this in a text??

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @saber said in qtreeview structure in a tex file:

                      i use qtreeview to create the file system

                      If you have the view you have the model

                      @VRonin said in qtreeview structure in a tex file:

                      then you can just QTextStream(textFile) << treeToText(myModel);

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1
                      • AbrarA Offline
                        AbrarA Offline
                        Abrar
                        wrote on last edited by
                        #11

                        I used the function of @VRonin but i am not understand what kind of model i need to pass to the function. Note that i have a model with which root path is root("/"). I tried to pass that model but it hanged my whole laptop. Please tell the right procedure to select the right QAbstractItemModel...
                        Thanks

                        1 Reply Last reply
                        0
                        • AbrarA Offline
                          AbrarA Offline
                          Abrar
                          wrote on last edited by Abrar
                          #12

                          Would you guys please help me to find out...
                          please.

                          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