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

I would appreciate any help. Thanks

J.YOLO
  • 41
  • 6
  • use the currentTextChanged signal that returns the selected item, after that you implement your logic with the QTextEdit – eyllanesc Jul 11 '18 at 06:17
  • use `connect(ui->comboBox, &QCombobox::currentTextChanged, ui->textEdit, &QTextEdit::append);` – eyllanesc Jul 11 '18 at 06:19
  • How can I list subdirectories from a chosen directory?I mean in" ui->textEdit, &QTextEdit::append)" how can I get the dir from combobox to list sub-dirs. The signal and slot that u mention above is right . But since I am a beginner ,can U show me some references? – J.YOLO Jul 11 '18 at 06:51
  • According to what I see, QComboBox has filenames, not directories. On the other hand whether or not it is a beginner the best reference is the documentation and the other second is SO, do not you think someone already wondered how to get the subdirectories of a given directory with Qt? the next question does something similar to what you're looking for https://stackoverflow.com/questions/8052460/recursively-iterate-over-all-the-files-in-a-directory-and-its-subdirectories-in, so the solution should be given by the QDir class or the QDirIterator class, check some methods there. – eyllanesc Jul 11 '18 at 06:56
  • I got the answer. – J.YOLO Jul 11 '18 at 07:33

1 Answers1

0

I got the answer .

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

    connect(ui->comboBox, &QComboBox::currentTextChanged,[this](const QString &selectedDirectory) {
        QDirIterator it(selectedDirectory,QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);

        ui->textEdit->clear();
        while (it.hasNext()){
            ui->textEdit->append(it.next());
        }

    });
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
J.YOLO
  • 41
  • 6