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