List Sub_directories from selected directory
-
I want to list subdirectories from inside a directory(only name,but also work as path) . This is the current code I use to list subdirs .
for(const QFileInfo & finfo: directory.entryInfoList()){ ui->listWidget_dirs->addItem(finfo.absoluteFilePath()); }
In that case I get the subdir paths.For example not like "home/user/Dir/subdir" , like"subdir". But I want to use this "subdir" to work like path. Because I need to use this subdir path to do something more. How can I do that?
-
@jsulm
Isn't it possible to show only subdir name ? But also work like a path . Ok , I need to use this selected directory and display images . So to display images , this selected subdir name must be a path . I don't want to list very long path . Thats why.
If you don't get what I mean or want to take alook , I can show you my full code . -
-
-
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();