Files
Kallery/imagegallery.cpp
2025-02-19 15:44:16 +01:00

47 lines
1.4 KiB
C++

#include "imagegallery.h"
#include <QPixmap>
#include <QImageReader>
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<QByteArray> 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());
}
}