diff --git a/imagegallery.cpp b/imagegallery.cpp index fc29b47..4aead1e 100644 --- a/imagegallery.cpp +++ b/imagegallery.cpp @@ -1,5 +1,6 @@ #include "imagegallery.h" #include +#include ImageGallery::ImageGallery(QWidget *parent) : QWidget(parent) { @@ -25,3 +26,21 @@ void ImageGallery::addImage(const QString &imagePath) 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()); + } +} diff --git a/imagegallery.h b/imagegallery.h index 3153373..464b6b3 100644 --- a/imagegallery.h +++ b/imagegallery.h @@ -6,6 +6,7 @@ #include #include #include +#include class ImageGallery : public QWidget { @@ -14,6 +15,7 @@ class ImageGallery : public QWidget public: explicit ImageGallery(QWidget *parent = nullptr); void addImage(const QString &imagePath); + void addImagesFromDirectory(const QString &directoryPath); private: QScrollArea *scrollArea; diff --git a/mainwindow.cpp b/mainwindow.cpp index 9bd2474..5ce9182 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,13 +1,12 @@ #include "mainwindow.h" - +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { imageGallery = new ImageGallery(this); setCentralWidget(imageGallery); - setWindowTitle("Image Gallery"); - imageGallery->addImage("/home/krzys/Obrazy/kod.png"); - imageGallery->addImage("/home/krzys/Obrazy/kod.png"); + setWindowTitle("Kallery Image Gallery"); + imageGallery->addImagesFromDirectory("/home/krzys/Obrazy"); resize(800, 600); }