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. Choose directory from combobox and list subdirectories in textedit
Forum Update on Monday, May 27th 2025

Choose directory from combobox and list subdirectories in textedit

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5.0comboboxlisttextedit
5 Posts 2 Posters 2.9k 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 last edited by
    #1

    I am trying to list all subdirectories which are inside a directory(which is chosen from combobox). Now I can show list of directories in combobox . I want to select one of those directory and list every sub-dirs( inside this selected dir) in textedit.
    I don't know how can I list the subdirectories when I choose a directory from combobox.Here is my current code.

     QDir directory = QFileDialog::getExistingDirectory(this, tr("Open  Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::
    DontResolveSymlinks);
    
        ui->comboBox->setMinimumWidth(500);
        QStringList files = directory.entryList(QDir::Files);
        ui->comboBox->addItems(files);
    
    aha_1980A 1 Reply Last reply
    0
    • K Kinesis

      I am trying to list all subdirectories which are inside a directory(which is chosen from combobox). Now I can show list of directories in combobox . I want to select one of those directory and list every sub-dirs( inside this selected dir) in textedit.
      I don't know how can I list the subdirectories when I choose a directory from combobox.Here is my current code.

       QDir directory = QFileDialog::getExistingDirectory(this, tr("Open  Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::
      DontResolveSymlinks);
      
          ui->comboBox->setMinimumWidth(500);
          QStringList files = directory.entryList(QDir::Files);
          ui->comboBox->addItems(files);
      
      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Kinesis,

      just connect a slot to the combo box currentTextChanged signal.

      Regards

      Qt has to stay free or it will die.

      K 1 Reply Last reply
      2
      • aha_1980A aha_1980

        Hi @Kinesis,

        just connect a slot to the combo box currentTextChanged signal.

        Regards

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

        @aha_1980
        I tried my code as U said but i don't know how can I get current directory in combobox to list in textEdit.Please show me the way. Here is the modified code.

        void Combo_box::on_pushButton_clicked()
        {
            QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
                             QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
        
            ui->comboBox->setMinimumWidth(500);
            for(const QFileInfo & finfo: directory.entryInfoList()){
            ui->comboBox->addItem(finfo.absoluteFilePath());
            auto textEdit = new QTextEdit();
        
            //connect(ui->comboBox, &QComboBox::currentTextChanged, ui->textEdit, &QTextEdit::append);
            connect(ui->comboBox, &QComboBox::currentTextChanged,[ textEdit,this]{
                QDirIterator it(directory,QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
        
                ui->textEdit->setText("");
                while (it.hasNext())
                {
                   ui->textEdit->append(it.next());
        
                }
           
                });
            }
        }
        
        1 Reply Last reply
        0
        • aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi @Kinesis,

          currentTextChanged(const QString &text) has the new text already as parameter, you need to capture it in your lambda:

          connect(ui->comboBox, &QComboBox::currentTextChanged,[textEdit,this](const QString &selectedDirectory) {
                  QDirIterator it(selectedDirectory,QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
          
                  ui->textEdit->clear();
                  while (it.hasNext())
                  {
                     ui->textEdit->append(it.next());
          
                  }
             
                  });
          

          You will of course need to work on your code in the lambda. I have not checked it.

          Qt has to stay free or it will die.

          K 1 Reply Last reply
          4
          • aha_1980A aha_1980

            Hi @Kinesis,

            currentTextChanged(const QString &text) has the new text already as parameter, you need to capture it in your lambda:

            connect(ui->comboBox, &QComboBox::currentTextChanged,[textEdit,this](const QString &selectedDirectory) {
                    QDirIterator it(selectedDirectory,QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
            
                    ui->textEdit->clear();
                    while (it.hasNext())
                    {
                       ui->textEdit->append(it.next());
            
                    }
               
                    });
            

            You will of course need to work on your code in the lambda. I have not checked it.

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

            @aha_1980
            It works ! Thanks alot
            :)

            1 Reply Last reply
            1

            • Login

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