How to filter image files from selected directory

2019-07-13 08:04发布

I have a problem that I don't know how to filter images from a selected directory. I usually use directory.setNameFilters({"*.png", "*.jpg"}); but in this case I can't use that because I need to use the selected directory inside a listWidget. I use signal and slot functions. I mean if I click a directory which is inside a listWidget, the images inside this directory will be displayed in another listWidget. If I click another directory it will do the same function. Please take a look my code.

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()){
    ui->listWidget_dirs->addItem(finfo.absoluteFilePath());
}

connect(ui->listWidget_dirs, & QListWidget::itemClicked,[listWidget_images,this](QListWidgetItem *item)
{
    listWidget_images->show();

    directory.setNameFilters({"*.png", "*.jpg"});

    for(const QFileInfo & finfo: directory.entryInfoList()){
        QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
        listWidget_images->addItem(item);
    }
});

1条回答
等我变得足够好
2楼-- · 2019-07-13 08:35

you have to create the QDir using the text of the QListWidgetItem.

On the other hand the method to use to obtain the list of subdirectories gets file and directories without distinguishing one from another, I have fixed that part.

QString directory = QFileDialog::getExistingDirectory(this,
                                                      tr("Open Directory"),
                                                      QDir::homePath(),
                                                      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);

QDirIterator diter(directory, QDir::Dirs | QDir::NoDotAndDotDot);

while (diter.hasNext()) {
    ui->listWidget_dirs->addItem(diter.next());
}


connect(ui->listWidget_dirs, &QListWidget::itemClicked,[listWidget_images](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);
    }
});
查看更多
登录 后发表回答