List Sub_directories from selected directory
-
-
Well , I did as you said . Now I can list only subdirs . But I can't use this name as file path . Because I added a click function to list images . If I click one of the subdir name, images will be displayed inside this subdir. But in that case I can't use the name . What should I do?Full code is here.
QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); auto listWidget_images = new QListWidget();//set listwidget to display images listWidget_images->setMinimumSize(1200,400); listWidget_images->setViewMode(QListWidget::IconMode); listWidget_images->setIconSize(QSize(320,240)); listWidget_images->setResizeMode(QListWidget::Adjust); for (const QFileInfo &finfo: directory.entryInfoList()) { QDir dir(finfo.absoluteFilePath()); ui->listWidget_dirs->addItem(dir.dirName()); connect(ui->listWidget_dirs, & QListWidget::itemClicked,[listWidget_images,this](QListWidgetItem *item) { listWidget_images->show(); listWidget_images->clear(); QDir directory(item->text()); directory.setNameFilters({"*.png", "*.jpg"}); for(const QFileInfo & finfo: directory.entryInfoList()){ QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName()); listWidget_images->addItem(item); } });
I can click the suddir names but the images won't be displayed. :(
-
@Kinesis said in List Sub_directories from selected directory:
QDir directory(item->text());
As I said above you need the parent directory, now you only have the last directory which is then relative path.
QDir directory(parentDir + '/' + item->text());
-
@Kinesis Sure you can:
QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); ... connect(ui->listWidget_dirs, & QListWidget::itemClicked,[directory, listWidget_images,this](QListWidgetItem *item) ...
-
@jsulm
Well , thanks . I added directory in capture list. But When I try to combine parent dir and child dir , "QDir path(directory + '/' + item->text());" , I got an error "invalid operands to binary expression ('QDir' and 'int')" . How can I fix that? -
@Kinesis said in List Sub_directories from selected directory:
How can I fix that?
You can read documentation...
QString dir = directory.absolutePath() + '/' + item->text();