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

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.8k 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 11 Jul 2018, 04:21 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);
    
    A 1 Reply Last reply 11 Jul 2018, 05:52
    0
    • K Kinesis
      11 Jul 2018, 04:21

      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);
      
      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 11 Jul 2018, 05:52 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 11 Jul 2018, 07:12
      2
      • A aha_1980
        11 Jul 2018, 05:52

        Hi @Kinesis,

        just connect a slot to the combo box currentTextChanged signal.

        Regards

        K Offline
        K Offline
        Kinesis
        wrote on 11 Jul 2018, 07:12 last edited by Kinesis 7 Nov 2018, 07:14
        #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
        • A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 11 Jul 2018, 07:18 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 11 Jul 2018, 07:32
          4
          • A aha_1980
            11 Jul 2018, 07:18

            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 11 Jul 2018, 07:32 last edited by
            #5

            @aha_1980
            It works ! Thanks alot
            :)

            1 Reply Last reply
            1

            3/5

            11 Jul 2018, 07:12

            • Login

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