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. List Sub_directories from selected directory

List Sub_directories from selected directory

Scheduled Pinned Locked Moved Solved General and Desktop
qt creatorqt5.5directorysubdirs
16 Posts 3 Posters 6.7k 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.
  • K Offline
    K Offline
    Kinesis
    wrote on 17 Jul 2018, 06:12 last edited by Kinesis
    #1

    I want to list subdirectories from inside a directory(only name,but also work as path) . This is the current code I use to list subdirs .

    for(const QFileInfo & finfo: directory.entryInfoList()){
           ui->listWidget_dirs->addItem(finfo.absoluteFilePath());
    
        }
    

    In that case I get the subdir paths.For example not like "home/user/Dir/subdir" , like"subdir". But I want to use this "subdir" to work like path. Because I need to use this subdir path to do something more. How can I do that?

    J 1 Reply Last reply 17 Jul 2018, 07:08
    0
    • K Kinesis
      17 Jul 2018, 06:12

      I want to list subdirectories from inside a directory(only name,but also work as path) . This is the current code I use to list subdirs .

      for(const QFileInfo & finfo: directory.entryInfoList()){
             ui->listWidget_dirs->addItem(finfo.absoluteFilePath());
      
          }
      

      In that case I get the subdir paths.For example not like "home/user/Dir/subdir" , like"subdir". But I want to use this "subdir" to work like path. Because I need to use this subdir path to do something more. How can I do that?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Jul 2018, 07:08 last edited by
      #2

      @Kinesis What stops you from doing

      ui->listWidget_dirs->addItem(directory.dirName() + '/' + finfo.absoluteFilePath());
      

      ?

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

      K 1 Reply Last reply 17 Jul 2018, 07:19
      0
      • J jsulm
        17 Jul 2018, 07:08

        @Kinesis What stops you from doing

        ui->listWidget_dirs->addItem(directory.dirName() + '/' + finfo.absoluteFilePath());
        

        ?

        K Offline
        K Offline
        Kinesis
        wrote on 17 Jul 2018, 07:19 last edited by
        #3

        @jsulm
        Isn't it possible to show only subdir name ? But also work like a path . Ok , I need to use this selected directory and display images . So to display images , this selected subdir name must be a path . I don't want to list very long path . Thats why.
        If you don't get what I mean or want to take alook , I can show you my full code .

        J 1 Reply Last reply 17 Jul 2018, 07:23
        0
        • K Kinesis
          17 Jul 2018, 07:19

          @jsulm
          Isn't it possible to show only subdir name ? But also work like a path . Ok , I need to use this selected directory and display images . So to display images , this selected subdir name must be a path . I don't want to list very long path . Thats why.
          If you don't get what I mean or want to take alook , I can show you my full code .

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Jul 2018, 07:23 last edited by
          #4

          @Kinesis If you don't want to show the whole path, then just schow the subdirectory, but keep the path to its parent directory to use later.

          /a/b/c
          /a/b/d
          /a/b/e
          

          Keep /a/b as parent and show only c, d and e.

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

          K 1 Reply Last reply 17 Jul 2018, 07:28
          2
          • J jsulm
            17 Jul 2018, 07:23

            @Kinesis If you don't want to show the whole path, then just schow the subdirectory, but keep the path to its parent directory to use later.

            /a/b/c
            /a/b/d
            /a/b/e
            

            Keep /a/b as parent and show only c, d and e.

            K Offline
            K Offline
            Kinesis
            wrote on 17 Jul 2018, 07:28 last edited by
            #5

            @jsulm
            Ah , I get what you mean . But how can I just show the subdirectory ?

            J 1 Reply Last reply 17 Jul 2018, 07:30
            0
            • K Kinesis
              17 Jul 2018, 07:28

              @jsulm
              Ah , I get what you mean . But how can I just show the subdirectory ?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 17 Jul 2018, 07:30 last edited by
              #6

              @Kinesis http://doc.qt.io/qt-5/qdir.html#dirName

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

              K 1 Reply Last reply 17 Jul 2018, 07:44
              3
              • J jsulm
                17 Jul 2018, 07:30

                @Kinesis http://doc.qt.io/qt-5/qdir.html#dirName

                K Offline
                K Offline
                Kinesis
                wrote on 17 Jul 2018, 07:44 last edited by
                #7

                @jsulm
                I don't know how to use"QDir::dirName() ". Please tell me the way.

                J A 2 Replies Last reply 17 Jul 2018, 07:48
                0
                • K Kinesis
                  17 Jul 2018, 07:44

                  @jsulm
                  I don't know how to use"QDir::dirName() ". Please tell me the way.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 17 Jul 2018, 07:48 last edited by
                  #8

                  @Kinesis Well:

                  QString dirPath = finfo.absoluteDir().dirName();
                  

                  You should read documentation and try.

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

                  1 Reply Last reply
                  4
                  • K Kinesis
                    17 Jul 2018, 07:44

                    @jsulm
                    I don't know how to use"QDir::dirName() ". Please tell me the way.

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 17 Jul 2018, 07:48 last edited by aha_1980
                    #9

                    @Kinesis:

                    for (const QFileInfo &finfo: directory.entryInfoList()) {
                      QDir dir(finfo.absoluteFilePath());
                      ui->listWidget_dirs->addItem(dir.dirName());
                    }
                    

                    disclaimer: only brain compiled

                    Edit: @jsulm has the better solution :)

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    4
                    • K Offline
                      K Offline
                      Kinesis
                      wrote on 17 Jul 2018, 08:03 last edited by
                      #10

                      Well , I did as you said . Now I can list only subdirs . But I can't use this name as file path . Because I added a click function to list images . If I click one of the subdir name, images will be displayed inside this subdir. But in that case I can't use the name . What should I do?Full code is here.

                      QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
                           
                          auto listWidget_images = new QListWidget();//set listwidget to display images
                          listWidget_images->setMinimumSize(1200,400);
                          listWidget_images->setViewMode(QListWidget::IconMode);
                          listWidget_images->setIconSize(QSize(320,240));
                          listWidget_images->setResizeMode(QListWidget::Adjust);
                      
                         
                        
                      for (const QFileInfo &finfo: directory.entryInfoList()) {
                           QDir dir(finfo.absoluteFilePath());
                           ui->listWidget_dirs->addItem(dir.dirName());
                      
                         
                      
                      
                          connect(ui->listWidget_dirs, & QListWidget::itemClicked,[listWidget_images,this](QListWidgetItem *item)
                          {
                              listWidget_images->show();
                              listWidget_images->clear();
                              QDir directory(item->text());
                              directory.setNameFilters({"*.png", "*.jpg"});
                              for(const QFileInfo & finfo: directory.entryInfoList()){
                                  QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
                                  listWidget_images->addItem(item);
                      
                              }
                          });
                      

                      I can click the suddir names but the images won't be displayed. :(

                      J 1 Reply Last reply 17 Jul 2018, 08:06
                      0
                      • K Kinesis
                        17 Jul 2018, 08:03

                        Well , I did as you said . Now I can list only subdirs . But I can't use this name as file path . Because I added a click function to list images . If I click one of the subdir name, images will be displayed inside this subdir. But in that case I can't use the name . What should I do?Full code is here.

                        QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
                             
                            auto listWidget_images = new QListWidget();//set listwidget to display images
                            listWidget_images->setMinimumSize(1200,400);
                            listWidget_images->setViewMode(QListWidget::IconMode);
                            listWidget_images->setIconSize(QSize(320,240));
                            listWidget_images->setResizeMode(QListWidget::Adjust);
                        
                           
                          
                        for (const QFileInfo &finfo: directory.entryInfoList()) {
                             QDir dir(finfo.absoluteFilePath());
                             ui->listWidget_dirs->addItem(dir.dirName());
                        
                           
                        
                        
                            connect(ui->listWidget_dirs, & QListWidget::itemClicked,[listWidget_images,this](QListWidgetItem *item)
                            {
                                listWidget_images->show();
                                listWidget_images->clear();
                                QDir directory(item->text());
                                directory.setNameFilters({"*.png", "*.jpg"});
                                for(const QFileInfo & finfo: directory.entryInfoList()){
                                    QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
                                    listWidget_images->addItem(item);
                        
                                }
                            });
                        

                        I can click the suddir names but the images won't be displayed. :(

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 17 Jul 2018, 08:06 last edited by
                        #11

                        @Kinesis said in List Sub_directories from selected directory:

                        QDir directory(item->text());

                        As I said above you need the parent directory, now you only have the last directory which is then relative path.

                         QDir directory(parentDir + '/' + item->text());
                        

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

                        K 1 Reply Last reply 17 Jul 2018, 08:40
                        1
                        • J jsulm
                          17 Jul 2018, 08:06

                          @Kinesis said in List Sub_directories from selected directory:

                          QDir directory(item->text());

                          As I said above you need the parent directory, now you only have the last directory which is then relative path.

                           QDir directory(parentDir + '/' + item->text());
                          
                          K Offline
                          K Offline
                          Kinesis
                          wrote on 17 Jul 2018, 08:40 last edited by
                          #12

                          @jsulm
                          I can't capture the parent directory which is selected from outside lambda . How can I capture it?

                          J 1 Reply Last reply 17 Jul 2018, 09:03
                          0
                          • K Kinesis
                            17 Jul 2018, 08:40

                            @jsulm
                            I can't capture the parent directory which is selected from outside lambda . How can I capture it?

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 17 Jul 2018, 09:03 last edited by
                            #13

                            @Kinesis Sure you can:

                            QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
                            ...
                            connect(ui->listWidget_dirs, & QListWidget::itemClicked,[directory, listWidget_images,this](QListWidgetItem *item)
                            ...
                            

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

                            K 1 Reply Last reply 18 Jul 2018, 03:43
                            2
                            • J jsulm
                              17 Jul 2018, 09:03

                              @Kinesis Sure you can:

                              QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
                              ...
                              connect(ui->listWidget_dirs, & QListWidget::itemClicked,[directory, listWidget_images,this](QListWidgetItem *item)
                              ...
                              
                              K Offline
                              K Offline
                              Kinesis
                              wrote on 18 Jul 2018, 03:43 last edited by
                              #14

                              @jsulm
                              Well , thanks . I added directory in capture list. But When I try to combine parent dir and child dir , "QDir path(directory + '/' + item->text());" , I got an error "invalid operands to binary expression ('QDir' and 'int')" . How can I fix that?

                              J 1 Reply Last reply 18 Jul 2018, 04:16
                              0
                              • K Kinesis
                                18 Jul 2018, 03:43

                                @jsulm
                                Well , thanks . I added directory in capture list. But When I try to combine parent dir and child dir , "QDir path(directory + '/' + item->text());" , I got an error "invalid operands to binary expression ('QDir' and 'int')" . How can I fix that?

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 18 Jul 2018, 04:16 last edited by
                                #15

                                @Kinesis said in List Sub_directories from selected directory:

                                How can I fix that?

                                You can read documentation...

                                QString dir = directory.absolutePath() + '/' + item->text();
                                
                                

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

                                K 1 Reply Last reply 18 Jul 2018, 04:19
                                2
                                • J jsulm
                                  18 Jul 2018, 04:16

                                  @Kinesis said in List Sub_directories from selected directory:

                                  How can I fix that?

                                  You can read documentation...

                                  QString dir = directory.absolutePath() + '/' + item->text();
                                  
                                  
                                  K Offline
                                  K Offline
                                  Kinesis
                                  wrote on 18 Jul 2018, 04:19 last edited by
                                  #16

                                  @jsulm
                                  got it got it . Thanks .

                                  1 Reply Last reply
                                  0

                                  5/16

                                  17 Jul 2018, 07:28

                                  topic:navigator.unread, 11
                                  • Login

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