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
Forum Updated to NodeBB v4.3 + New Features

List Sub_directories from selected directory

Scheduled Pinned Locked Moved Solved General and Desktop
qt creatorqt5.5directorysubdirs
16 Posts 3 Posters 7.2k 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.
  • K Kinesis

    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?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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
    0
    • jsulmJ jsulm

      @Kinesis What stops you from doing

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

      ?

      K Offline
      K Offline
      Kinesis
      wrote on 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 .

      jsulmJ 1 Reply Last reply
      0
      • K Kinesis

        @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 .

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        2
        • jsulmJ jsulm

          @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 last edited by
          #5

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

          jsulmJ 1 Reply Last reply
          0
          • K Kinesis

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            3
            • jsulmJ jsulm

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

              K Offline
              K Offline
              Kinesis
              wrote on last edited by
              #7

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

              jsulmJ aha_1980A 2 Replies Last reply
              0
              • K Kinesis

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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

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

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 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 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. :(

                    jsulmJ 1 Reply Last reply
                    0
                    • K Kinesis

                      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. :(

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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
                      1
                      • jsulmJ jsulm

                        @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 last edited by
                        #12

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

                        jsulmJ 1 Reply Last reply
                        0
                        • K Kinesis

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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 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
                          2
                          • jsulmJ jsulm

                            @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 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?

                            jsulmJ 1 Reply Last reply
                            0
                            • K Kinesis

                              @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?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 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
                              2
                              • jsulmJ jsulm

                                @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 last edited by
                                #16

                                @jsulm
                                got it got it . Thanks .

                                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