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. help in QDirIterator to a text file

help in QDirIterator to a text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 4.1k 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.
  • S Offline
    S Offline
    saber
    wrote on 13 Jul 2018, 04:16 last edited by saber
    #1

    i wanted to get a folder structure to a text file like this

    .
    ├── app
    │   ├── about
    │   │   ├── about.cpp
    │   │   ├── about.h
    │   │   └── about.ui
    │   ├── app.pro
    │   ├── bookmarks
    │   │   ├── bookmarkdialog.cpp
    │   │   ├── bookmarkdialog.h
    │   │   ├── bookmarkmanage.cpp
    │   │   ├── bookmarkmanage.h
    │   │   ├── bookmarks.cpp
    │   │   ├── bookmarks.h
    │   │   └── bookmarks.ui
    │   ├── coreaction
    │   │   ├── coreaction.cpp
    │   │   ├── coreaction.h
    │   │   └── coreaction.ui
    │   ├── corebox
    │   │   ├── corebox.cpp
    │   │   ├── corebox.h
    │   │   ├── corebox.ui
    │   │   ├── globalfunctions.cpp
    │   │   └── globalfunctions.h
    │   ├── coreimage
    │   │   ├── coreimage.cpp
    │   │   ├── coreimage.h
    │   │   └── coreimage.ui
    │   ├── corepad
    │   │   ├── coreedit.cpp
    │   │   ├── coreedit.h
    │   │   ├── corepad.cpp
    │   │   ├── corepad.h
    │   │   └── corepad.ui
    │   ├── corepdf
    │   │   ├── corepdf.cpp
    │   │   └── corepdf.h
    │   ├── coreplayer
    │   │   ├── coreplayer.cpp
    │   │   ├── coreplayer.h
    │   │   └── coreplayer.ui
    │   ├── corescreenshot
    │   │   ├── mainwindow.cpp
    │   │   ├── mainwindow.h
    │   │   ├── modefullscreen.cpp
    │   │   ├── modefullscreen.h
    │   │   ├── modeselectarea.cpp
    │   │   ├── modeselectarea.h
    │   │   ├── previewwidget.cpp
    │   │   ├── previewwidget.h
    │   │   ├── rectarea.cpp
    │   │   ├── rectarea.h
    │   │   ├── screenwidget.cpp
    │   │   └── screenwidget.h
    │   ├── coretime
    │   │   ├── alarm.cpp
    │   │   ├── alarm.h
    │   │   ├── coretime.cpp
    │   │   ├── coretime.h
    │   │   ├── coretime.ui
    │   │   ├── fileio.cpp
    │   │   ├── fileio.h
    │   │   ├── schedulecollection.cpp
    │   │   ├── schedulecollection.h
    │   │   ├── schedule.cpp
    │   │   ├── schedule.h
    │   │   ├── snooze.cpp
    │   │   ├── snooze.h
    │   │   ├── snooze.ui
    │   │   ├── timer.cpp
    │   │   └── timer.h
    │   ├── help
    │   │   ├── help.cpp
    │   │   ├── help.h
    │   │   └── help.ui
    │   ├── icons.qrc
    │   ├── main.cpp
    │   ├── other
    │   │   ├── background.png
    │   │   └── sound.ogg
    │   ├── other.qrc
    │   ├── README.md
    │   ├── search
    │   │   ├── search.cpp
    │   │   ├── search.h
    │   │   └── search.ui
    │   ├── settings
    │   │   ├── settings.cpp
    │   │   ├── settings.h
    │   │   ├── settingsmanage.cpp
    │   │   ├── settingsmanage.h
    │   │   └── settings.ui
    │   └── start
    │       ├── start.cpp
    │       ├── start.h
    │       └── start.ui
    ├── CoreBox.desktop
    ├── CoreBox.png
    ├── CoreBox.pro
    ├── docs
    │   ├── buildinfo.txt
    │   ├── ChangeLog
    │   ├── docs.qrc
    │   ├── PKGBUILD
    │   ├── ReleaseNotes
    │   ├── screenshots
    │   │   ├── a1.png
    │   │   ├── a2.png
    │   │   ├── a3.png
    │   │   └── a4.png
    │   └── To-Do.txt
    ├── LICENSE
    ├── README.md
    └── t.txt
    
    18 directories, 96 files
    
    

    so made a code with QDirIterator . here is the code

    QString dirItemToText::getFolderConts(const QString &path)
    {
    
        QString output;
    
        QString currentPath;
        QDirIterator it(path, QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden, QDirIterator::Subdirectories);
        while (it.hasNext()) {
            it.next();
            if(it.fileInfo().isDir()) {
                if (it.filePath() == path)
                    continue;
    
                currentPath = it.fileInfo().baseName();
                output += "├── " + it.fileInfo().baseName() + "\n";
            } else {
                if (it.fileInfo().baseName() == currentPath)
                    output += "│   ├── " + it.fileName() + "\n";
    
            }
        }
        qDebug () << output;
    
        QString filePath = "/home/shaber/y.txt";
    
        QFile file(filePath);
        file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
        QTextStream text(&file);
        text << output;
        file.close();
    }
    

    but the output is not like what i wanted
    my codes output is this and at l last couple of line i get some garbage

    │   ├── .travis.yml
    ├── app
    ├── start
    │   ├── start.cpp
    │   ├── start.h
    │   ├── start.ui
    ├── coreimage
    │   ├── coreimage.ui
    │   ├── coreimage.cpp
    │   ├── coreimage.h
    ├── settings
    │   ├── settings.h
    │   ├── settings.cpp
    │   ├── settings.ui
    ├── about
    │   ├── about.cpp
    │   ├── about.ui
    │   ├── about.h
    ├── help
    │   ├── help.h
    │   ├── help.cpp
    │   ├── help.ui
    ├── corepad
    │   ├── corepad.h
    │   ├── corepad.ui
    │   ├── corepad.cpp
    ├── corebox
    │   ├── corebox.cpp
    │   ├── corebox.ui
    │   ├── corebox.h
    ├── search
    │   ├── search.ui
    │   ├── search.h
    │   ├── search.cpp
    ├── corepdf
    │   ├── corepdf.h
    │   ├── corepdf.cpp
    ├── coretime
    │   ├── coretime.cpp
    │   ├── coretime.h
    │   ├── coretime.ui
    ├── coreaction
    │   ├── coreaction.h
    │   ├── coreaction.ui
    │   ├── coreaction.cpp
    ├── corescreenshot
    ├── coreplayer
    │   ├── coreplayer.cpp
    │   ├── coreplayer.ui
    │   ├── coreplayer.h
    ├── bookmarks
    │   ├── bookmarks.ui
    │   ├── bookmarks.h
    │   ├── bookmarks.cpp
    ├── other
    ├── docs
    ├── screenshots
    ├── 
    ├── branches
    ├── refs
    ├── heads
    ├── tags
    ├── remotes
    ├── origin
    ├── hooks
    ├── objects
    ├── 99
    ├── 73
    ├── bd
    ├── da
    ├── 9d
    ├── 2f
    ├── info
    ├── 26
    ├── 7f
    ├── 41
    ├── 74
    ├── 2b
    ├── 6c
    ├── 34
    ├── ff
    ├── pack
    ├── fd
    ├── 48
    ├── 3d
    ├── info
    ├── logs
    ├── refs
    ├── heads
    ├── remotes
    ├── origin
    
    

    i have no idea how to get output like the first one and why i am getting the garbage.
    also some time it crashes .
    please help.

    jsulmJ 1 Reply Last reply 13 Jul 2018, 04:25
    0
    • S saber
      13 Jul 2018, 04:16

      i wanted to get a folder structure to a text file like this

      .
      ├── app
      │   ├── about
      │   │   ├── about.cpp
      │   │   ├── about.h
      │   │   └── about.ui
      │   ├── app.pro
      │   ├── bookmarks
      │   │   ├── bookmarkdialog.cpp
      │   │   ├── bookmarkdialog.h
      │   │   ├── bookmarkmanage.cpp
      │   │   ├── bookmarkmanage.h
      │   │   ├── bookmarks.cpp
      │   │   ├── bookmarks.h
      │   │   └── bookmarks.ui
      │   ├── coreaction
      │   │   ├── coreaction.cpp
      │   │   ├── coreaction.h
      │   │   └── coreaction.ui
      │   ├── corebox
      │   │   ├── corebox.cpp
      │   │   ├── corebox.h
      │   │   ├── corebox.ui
      │   │   ├── globalfunctions.cpp
      │   │   └── globalfunctions.h
      │   ├── coreimage
      │   │   ├── coreimage.cpp
      │   │   ├── coreimage.h
      │   │   └── coreimage.ui
      │   ├── corepad
      │   │   ├── coreedit.cpp
      │   │   ├── coreedit.h
      │   │   ├── corepad.cpp
      │   │   ├── corepad.h
      │   │   └── corepad.ui
      │   ├── corepdf
      │   │   ├── corepdf.cpp
      │   │   └── corepdf.h
      │   ├── coreplayer
      │   │   ├── coreplayer.cpp
      │   │   ├── coreplayer.h
      │   │   └── coreplayer.ui
      │   ├── corescreenshot
      │   │   ├── mainwindow.cpp
      │   │   ├── mainwindow.h
      │   │   ├── modefullscreen.cpp
      │   │   ├── modefullscreen.h
      │   │   ├── modeselectarea.cpp
      │   │   ├── modeselectarea.h
      │   │   ├── previewwidget.cpp
      │   │   ├── previewwidget.h
      │   │   ├── rectarea.cpp
      │   │   ├── rectarea.h
      │   │   ├── screenwidget.cpp
      │   │   └── screenwidget.h
      │   ├── coretime
      │   │   ├── alarm.cpp
      │   │   ├── alarm.h
      │   │   ├── coretime.cpp
      │   │   ├── coretime.h
      │   │   ├── coretime.ui
      │   │   ├── fileio.cpp
      │   │   ├── fileio.h
      │   │   ├── schedulecollection.cpp
      │   │   ├── schedulecollection.h
      │   │   ├── schedule.cpp
      │   │   ├── schedule.h
      │   │   ├── snooze.cpp
      │   │   ├── snooze.h
      │   │   ├── snooze.ui
      │   │   ├── timer.cpp
      │   │   └── timer.h
      │   ├── help
      │   │   ├── help.cpp
      │   │   ├── help.h
      │   │   └── help.ui
      │   ├── icons.qrc
      │   ├── main.cpp
      │   ├── other
      │   │   ├── background.png
      │   │   └── sound.ogg
      │   ├── other.qrc
      │   ├── README.md
      │   ├── search
      │   │   ├── search.cpp
      │   │   ├── search.h
      │   │   └── search.ui
      │   ├── settings
      │   │   ├── settings.cpp
      │   │   ├── settings.h
      │   │   ├── settingsmanage.cpp
      │   │   ├── settingsmanage.h
      │   │   └── settings.ui
      │   └── start
      │       ├── start.cpp
      │       ├── start.h
      │       └── start.ui
      ├── CoreBox.desktop
      ├── CoreBox.png
      ├── CoreBox.pro
      ├── docs
      │   ├── buildinfo.txt
      │   ├── ChangeLog
      │   ├── docs.qrc
      │   ├── PKGBUILD
      │   ├── ReleaseNotes
      │   ├── screenshots
      │   │   ├── a1.png
      │   │   ├── a2.png
      │   │   ├── a3.png
      │   │   └── a4.png
      │   └── To-Do.txt
      ├── LICENSE
      ├── README.md
      └── t.txt
      
      18 directories, 96 files
      
      

      so made a code with QDirIterator . here is the code

      QString dirItemToText::getFolderConts(const QString &path)
      {
      
          QString output;
      
          QString currentPath;
          QDirIterator it(path, QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden, QDirIterator::Subdirectories);
          while (it.hasNext()) {
              it.next();
              if(it.fileInfo().isDir()) {
                  if (it.filePath() == path)
                      continue;
      
                  currentPath = it.fileInfo().baseName();
                  output += "├── " + it.fileInfo().baseName() + "\n";
              } else {
                  if (it.fileInfo().baseName() == currentPath)
                      output += "│   ├── " + it.fileName() + "\n";
      
              }
          }
          qDebug () << output;
      
          QString filePath = "/home/shaber/y.txt";
      
          QFile file(filePath);
          file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
          QTextStream text(&file);
          text << output;
          file.close();
      }
      

      but the output is not like what i wanted
      my codes output is this and at l last couple of line i get some garbage

      │   ├── .travis.yml
      ├── app
      ├── start
      │   ├── start.cpp
      │   ├── start.h
      │   ├── start.ui
      ├── coreimage
      │   ├── coreimage.ui
      │   ├── coreimage.cpp
      │   ├── coreimage.h
      ├── settings
      │   ├── settings.h
      │   ├── settings.cpp
      │   ├── settings.ui
      ├── about
      │   ├── about.cpp
      │   ├── about.ui
      │   ├── about.h
      ├── help
      │   ├── help.h
      │   ├── help.cpp
      │   ├── help.ui
      ├── corepad
      │   ├── corepad.h
      │   ├── corepad.ui
      │   ├── corepad.cpp
      ├── corebox
      │   ├── corebox.cpp
      │   ├── corebox.ui
      │   ├── corebox.h
      ├── search
      │   ├── search.ui
      │   ├── search.h
      │   ├── search.cpp
      ├── corepdf
      │   ├── corepdf.h
      │   ├── corepdf.cpp
      ├── coretime
      │   ├── coretime.cpp
      │   ├── coretime.h
      │   ├── coretime.ui
      ├── coreaction
      │   ├── coreaction.h
      │   ├── coreaction.ui
      │   ├── coreaction.cpp
      ├── corescreenshot
      ├── coreplayer
      │   ├── coreplayer.cpp
      │   ├── coreplayer.ui
      │   ├── coreplayer.h
      ├── bookmarks
      │   ├── bookmarks.ui
      │   ├── bookmarks.h
      │   ├── bookmarks.cpp
      ├── other
      ├── docs
      ├── screenshots
      ├── 
      ├── branches
      ├── refs
      ├── heads
      ├── tags
      ├── remotes
      ├── origin
      ├── hooks
      ├── objects
      ├── 99
      ├── 73
      ├── bd
      ├── da
      ├── 9d
      ├── 2f
      ├── info
      ├── 26
      ├── 7f
      ├── 41
      ├── 74
      ├── 2b
      ├── 6c
      ├── 34
      ├── ff
      ├── pack
      ├── fd
      ├── 48
      ├── 3d
      ├── info
      ├── logs
      ├── refs
      ├── heads
      ├── remotes
      ├── origin
      
      

      i have no idea how to get output like the first one and why i am getting the garbage.
      also some time it crashes .
      please help.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 13 Jul 2018, 04:25 last edited by
      #2

      @saber It can't work like this: the amount of indentations depends on the number of subdirectories relative to path. You now have fix indentations "|--" and "| |--".

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

      S 1 Reply Last reply 13 Jul 2018, 04:31
      2
      • jsulmJ jsulm
        13 Jul 2018, 04:25

        @saber It can't work like this: the amount of indentations depends on the number of subdirectories relative to path. You now have fix indentations "|--" and "| |--".

        S Offline
        S Offline
        saber
        wrote on 13 Jul 2018, 04:31 last edited by saber
        #3

        @jsulm ok.
        but can't figure it out where to add another "| " in code.

        my head is exploding . please help!!!

        JonBJ 1 Reply Last reply 13 Jul 2018, 07:13
        0
        • S saber
          13 Jul 2018, 04:31

          @jsulm ok.
          but can't figure it out where to add another "| " in code.

          my head is exploding . please help!!!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 13 Jul 2018, 07:13 last edited by
          #4

          @saber
          Do you know what recursion is? You are trying to walk an arbitrary-depth tree, outputting as you go along according to depth. Writing it recursively will result in less "head explosion" than trying to do it iteratively.

          S 1 Reply Last reply 13 Jul 2018, 14:59
          4
          • JonBJ JonB
            13 Jul 2018, 07:13

            @saber
            Do you know what recursion is? You are trying to walk an arbitrary-depth tree, outputting as you go along according to depth. Writing it recursively will result in less "head explosion" than trying to do it iteratively.

            S Offline
            S Offline
            saber
            wrote on 13 Jul 2018, 14:59 last edited by
            #5

            @JonB
            no idea .
            help with code please.
            i have to submit a project so i need this working.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              saber
              wrote on 14 Jul 2018, 07:33 last edited by saber
              #6

              can @mrjj help me? please

              mrjjM 1 Reply Last reply 15 Jul 2018, 08:28
              0
              • S saber
                14 Jul 2018, 07:33

                can @mrjj help me? please

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 15 Jul 2018, 08:28 last edited by
                #7

                @saber
                Sorry i have no idea how to get depth with QDirIterator and if you dont know what
                recursion is, then it will be hard to understand the code anyway.

                What about just calling linux tree command with QProcess and capture output ?
                alt text

                S 1 Reply Last reply 15 Jul 2018, 09:38
                1
                • mrjjM mrjj
                  15 Jul 2018, 08:28

                  @saber
                  Sorry i have no idea how to get depth with QDirIterator and if you dont know what
                  recursion is, then it will be hard to understand the code anyway.

                  What about just calling linux tree command with QProcess and capture output ?
                  alt text

                  S Offline
                  S Offline
                  saber
                  wrote on 15 Jul 2018, 09:38 last edited by saber
                  #8

                  @mrjj
                  i know that way .but not every distro come with that pre-installed and also i got some problem with qt to capture output .here

                  can you please review my code?
                  i just know that i need to add another line if the folder has any item .but can't figure it how in code.
                  it is important .it will really helpful if it is working.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    saber
                    wrote on 16 Jul 2018, 15:09 last edited by
                    #9

                    nothing yet

                    mrjjM M 2 Replies Last reply 16 Jul 2018, 15:32
                    0
                    • S saber
                      16 Jul 2018, 15:09

                      nothing yet

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 16 Jul 2018, 15:32 last edited by
                      #10

                      @saber
                      Hi i dont know how to make it.
                      Im not sure QDirIterator can say if any files in folder.
                      As far as i know its one huge list.

                      1 Reply Last reply
                      0
                      • S saber
                        16 Jul 2018, 15:09

                        nothing yet

                        M Offline
                        M Offline
                        mpergand
                        wrote on 16 Jul 2018, 20:35 last edited by
                        #11

                        @saber
                        You need a recursive algorithm, no escape from this ...

                        Get ride of QDirIterator::Subdirectories
                        and call your method for each directory:

                        if(it.fileInfo().isDir())
                            getFolder(it.filePath()); // recursive call
                        

                        Extra work is needed for displaying the indentation symbols.
                        -test if item is the last in this directory (├─ or └─ )
                        -same test for directory item (add │ or space to the indentation string)
                        -a way to retreive this indentation string over each recusive call.

                        Not so hard, you can do it ;)

                        JonBJ 1 Reply Last reply 17 Jul 2018, 08:20
                        5
                        • M mpergand
                          16 Jul 2018, 20:35

                          @saber
                          You need a recursive algorithm, no escape from this ...

                          Get ride of QDirIterator::Subdirectories
                          and call your method for each directory:

                          if(it.fileInfo().isDir())
                              getFolder(it.filePath()); // recursive call
                          

                          Extra work is needed for displaying the indentation symbols.
                          -test if item is the last in this directory (├─ or └─ )
                          -same test for directory item (add │ or space to the indentation string)
                          -a way to retreive this indentation string over each recusive call.

                          Not so hard, you can do it ;)

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 17 Jul 2018, 08:20 last edited by
                          #12

                          @mpergand said in help in QDirIterator to a text file:

                          You need a recursive algorithm, no escape from this ...

                          For the record, one can always write a recursive algorithm iteratively :) But as I suggested earlier OP would be better writing it recursively, as per your outline, to minimize brain-ache!

                          1 Reply Last reply
                          1
                          • AbrarA Offline
                            AbrarA Offline
                            Abrar
                            wrote on 17 Jul 2018, 17:27 last edited by Abrar
                            #13

                            I created a function but don't know why whole app is crashing. Plz help on that...
                            Code:

                            QString getTree(const QString &path) {
                            QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks);

                            while (it.hasNext()) {
                            it.next();
                            if (it.fileInfo().isDir()) {
                            qDebug() << it.fileInfo().path();
                            getTree(it.filePath());
                            }

                            qDebug() << "\t" << it.filePath();
                            }
                            }

                            JonBJ 1 Reply Last reply 17 Jul 2018, 17:29
                            0
                            • AbrarA Abrar
                              17 Jul 2018, 17:27

                              I created a function but don't know why whole app is crashing. Plz help on that...
                              Code:

                              QString getTree(const QString &path) {
                              QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks);

                              while (it.hasNext()) {
                              it.next();
                              if (it.fileInfo().isDir()) {
                              qDebug() << it.fileInfo().path();
                              getTree(it.filePath());
                              }

                              qDebug() << "\t" << it.filePath();
                              }
                              }

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on 17 Jul 2018, 17:29 last edited by
                              #14

                              @Abrar
                              while (it.hasNext) {

                              I don't do C++, but shouldn't that be it.hasNext()?

                              AbrarA 1 Reply Last reply 17 Jul 2018, 17:49
                              0
                              • M Offline
                                M Offline
                                mpergand
                                wrote on 17 Jul 2018, 17:35 last edited by mpergand
                                #15

                                You return nothing, that's the cause.

                                @JonB
                                Your're right ofcourse, in fact it doesn"t even compile.

                                AbrarA 1 Reply Last reply 17 Jul 2018, 17:38
                                1
                                • M mpergand
                                  17 Jul 2018, 17:35

                                  You return nothing, that's the cause.

                                  @JonB
                                  Your're right ofcourse, in fact it doesn"t even compile.

                                  AbrarA Offline
                                  AbrarA Offline
                                  Abrar
                                  wrote on 17 Jul 2018, 17:38 last edited by
                                  #16

                                  @mpergand thanks a lot it's working

                                  1 Reply Last reply
                                  0
                                  • JonBJ JonB
                                    17 Jul 2018, 17:29

                                    @Abrar
                                    while (it.hasNext) {

                                    I don't do C++, but shouldn't that be it.hasNext()?

                                    AbrarA Offline
                                    AbrarA Offline
                                    Abrar
                                    wrote on 17 Jul 2018, 17:49 last edited by
                                    #17

                                    @JonB Yes you are right.
                                    That was my typing mistake.
                                    @mpergand it was compiling.

                                    JonBJ 1 Reply Last reply 17 Jul 2018, 17:54
                                    0
                                    • AbrarA Abrar
                                      17 Jul 2018, 17:49

                                      @JonB Yes you are right.
                                      That was my typing mistake.
                                      @mpergand it was compiling.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on 17 Jul 2018, 17:54 last edited by JonB
                                      #18

                                      @Abrar
                                      Never type "your code", always copy & paste! That would save a fair proportion of questions/answers in the forum!

                                      AbrarA 1 Reply Last reply 17 Jul 2018, 17:59
                                      1
                                      • JonBJ JonB
                                        17 Jul 2018, 17:54

                                        @Abrar
                                        Never type "your code", always copy & paste! That would save a fair proportion of questions/answers in the forum!

                                        AbrarA Offline
                                        AbrarA Offline
                                        Abrar
                                        wrote on 17 Jul 2018, 17:59 last edited by
                                        #19

                                        @JonB thanks for your suggestion.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          saber
                                          wrote on 8 Aug 2018, 07:26 last edited by saber 8 Oct 2018, 03:58
                                          #20

                                          @Abrar made a function that works .but problems is it takes a long time to make the text file (not enter every folder and get the name).it takes long time if the folder tree is complex.

                                          i wonder how the tree package that @mrjj pointing is take fraction of the time to make that same text file.

                                          here is the code.

                                          int pat = 0;
                                          QString strDir;
                                          QString getFileFolderTree(const QString &path)
                                          {
                                              
                                              // ├
                                          
                                              // └
                                          
                                              // │
                                          
                                              // ──
                                          
                                          
                                              pat++;
                                          
                                              QDir dir(path);
                                              //dir.setSorting(QDir::DirsFirst);
                                              //dir.setNameFilters(QStringList("*"));
                                              dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
                                              QStringList dList = dir.entryList();
                                              for (int i = 0; i < dList.count(); ++i) {
                                                  QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dList.at(i));
                                          
                                                  QString strPat = "├── ";
                                                  for (int i = 1; i < pat; i++) {
                                                      strPat.insert(0, "│   ");
                                                  }
                                          
                                                  if (i == dList.count() - 1) {
                                                      int pos = strPat.count() - 4;
                                                      strPat = strPat.replace(pos, 1, "└");
                                                  }
                                          
                                                  strDir.append(strPat + QDir(dList.at(i)).dirName() + "\n");
                                          
                                                  getFileFolderTree(newPath);
                                              }
                                          
                                              pat--;
                                          
                                              dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
                                              QStringList fList = dir.entryList();
                                              for (int i = 0; i < fList.count(); i++) {
                                          
                                                  QString strPat = "├── ";
                                                  for (int j = 1; j < pat + 1; j++) {
                                                      strPat.insert(0, "│   ");
                                                  }
                                          
                                                  if (i == fList.count() - 1) {
                                                      strPat = strPat.replace(strPat.count() - 4, 1, "└");
                                                  }
                                          
                                                  strDir.append(strPat + fList[i] + "\n");
                                              }
                                          
                                          //    QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System);
                                          //    while (it.hasNext()) {
                                          //        it.next();
                                          //        if (it.fileInfo().isDir()) {
                                          //            qDebug() << it.fileInfo().path();
                                          //            getFileFolderTree(it.filePath());
                                          //        }
                                          //        qDebug() << "\t" << it.filePath();
                                          //    }
                                          
                                              //qDebug() << strDir;
                                          
                                              if (!strDir.count())
                                                  strDir.append(".\n");
                                          
                                              QFile file("/home/abrar/Desktop/UI.txt");
                                              file.open(QIODevice::Text | QIODevice::ReadWrite | QIODevice::Truncate);
                                              QTextStream out(&file);
                                              out << strDir;
                                              file.close();
                                          
                                          
                                              return QString();
                                          }
                                          
                                          

                                          so i need some help to dicress the time to make the text file .
                                          help! help! help!
                                          thanks.

                                          JonBJ 1 Reply Last reply 8 Aug 2018, 09:40
                                          0

                                          • Login

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