Choose directory from combobox and list subdirectories in textedit



  • 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);
    

  • Qt Champions 2017

    Hi @Kinesis,

    just connect a slot to the combo box currentTextChanged signal.

    Regards



  • @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());
    
            }
       
            });
        }
    }
    

  • Qt Champions 2017

    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.



  • @aha_1980
    It works ! Thanks alot
    :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.