Choose directory from combobox and list subdirectories in textedit
Solved
General and Desktop
-
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
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()); } }); } }
-
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.