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

                      16/16

                      18 Jul 2018, 04:19

                      • Login

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