import directory

This commit is contained in:
2025-02-19 15:44:16 +01:00
parent c4d2e44836
commit 5c41adf907
3 changed files with 24 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
#include "imagegallery.h"
#include <QPixmap>
#include <QImageReader>
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<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());
}
}

View File

@@ -6,6 +6,7 @@
#include <QVBoxLayout>
#include <QLabel>
#include <QList>
#include <QDir>
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;

View File

@@ -1,13 +1,12 @@
#include "mainwindow.h"
#include <QApplication>
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);
}