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 iterate all sub-folders of a directory?

How to iterate all sub-folders of a directory?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.8k Views 1 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.
  • X Offline
    X Offline
    xalam
    wrote on last edited by
    #1

    Hi, I am showing file same in tree view using QDirModel. The user can select a given folder and my application need to traverse all its subfolders (only one level). I haven't been able to figure out how but here is where I am stuck.

    @// I can get the index of the current selected folder
    QModelIndex index = ui->treeView->currentIndex();@

    Now else where I see I could use something like this:

    @ QDirIterator it("/etc", QDirIterator::Subdirectories);
    while (it.hasNext()) {
    qDebug() << it.next();

     // /etc/.
     // /etc/..
     // /etc/X11
     // /etc/X11/fs
     // ...
    

    }@

    The problem is how to get the currently selected for as QDir object from tree and list view and feed it to the 2nd block of code?

    Initially I used QFileSystemModel which I know is recommended now instead of QDir but it proved more complicated. I would prefer solution with QFileSystemModel but QDirModel is also acceptable.

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

      Hi,

      Do you mean "QFileSystemModel::filePath":http://doc.qt.io/qt-5/qfilesystemmodel.html#filePath ?

      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
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        You can get the directory from the special roles the QFileSystemModel supplies for you:
        @
        enum Roles { FileIconRole, FilePathRole, FileNameRole, FilePermissions }
        @

        Sound to me like the FilePathRole provides a complete path. Using QFileInfo, you can easily figure out if the current item is a file or a dir, and what the path to use is.

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xalam
          wrote on last edited by
          #4

          Sorry guys, I was not thinking right. I figured it all out. Here is what I and I did use QFileSystemModel.

          @void MainWindow::on_actionExport_files_triggered()
          {
          QModelIndex index = ui->treeView->currentIndex();

          QString rootDir = dirModel->filePath( selectedIndex );
          
          QDirIterator iter( rootDir, QDir::Dirs | QDir::NoDotAndDotDot);
          
          while(iter.hasNext() )
          {
              qDebug() << iter.next();
          }
          

          ........
          }@

          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