#include "imagegallery.h" #include #include ImageGallery::ImageGallery(QWidget *parent) : QWidget(parent) { scrollArea = new QScrollArea(this); scrollWidget = new QWidget(scrollArea); scrollLayout = new QVBoxLayout(scrollWidget); scrollArea->setWidgetResizable(true); scrollArea->setWidget(scrollWidget); QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(scrollArea); setLayout(mainLayout); } void ImageGallery::addImage(const QString &imagePath) { QLabel *imageLabel = new QLabel(scrollWidget); QPixmap pixmap(imagePath); imageLabel->setPixmap(pixmap.scaledToWidth(400, Qt::SmoothTransformation)); imageLabel->setAlignment(Qt::AlignCenter); scrollLayout->addWidget(imageLabel); imageLabels.append(imageLabel); } void ImageGallery::addImagesFromDirectory(const QString &directoryPath){ QDir directory(directoryPath); QStringList filters; const QList supportedFormats = QImageReader::supportedImageFormats(); for(const QByteArray &format : supportedFormats){ filters.append("*." + QString(format).toLower()); } directory.setNameFilters(filters); directory.setFilter(QDir::Files | QDir::Readable); directory.setSorting(QDir::Name); const QFileInfoList fileList = directory.entryInfoList(); for(const QFileInfo &fileInfo : fileList) { addImage(fileInfo.absoluteFilePath()); } }