Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Choose directory from combobox and list subdirectories in textedit

    General and Desktop
    qt5.5.0 combobox list textedit
    2
    5
    1775
    Loading More Posts
    • 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 last edited by

      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_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @Kinesis last edited by

        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 Reply Quote 2
        • K
          Kinesis @aha_1980 last edited by Kinesis

          @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 Reply Quote 0
          • aha_1980
            aha_1980 Lifetime Qt Champion last edited by

            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 Reply Quote 4
            • K
              Kinesis @aha_1980 last edited by

              @aha_1980
              It works ! Thanks alot
              :)

              1 Reply Last reply Reply Quote 1
              • First post
                Last post