How to filter image files from selected directory (which is inside listWidget)

左心房为你撑大大i 提交于 2019-12-24 01:38:08

问题


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:


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


来源:https://stackoverflow.com/questions/51356319/how-to-filter-image-files-from-selected-directory-which-is-inside-listwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!