Search code examples
c++qtdspace

Load images from folder with Qt


i am new in QT and i want to display a lot of images, and be able to load them from a folder, i have a code for only one image, it worked fine but want to have the same result for many images , here is my code :

 QString imagePath = QFileDialog::getOpenFileName(
            this,
            tr("Open File"),
            "",
            tr("JPEG (*.jpg *.jpeg);;PNG (*.png);; BMP (*.bmp)" )
            );
imageObject = new QImage();
imageObject->load(imagePath);
image = QPixmap::fromImage(*imageObject);
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
ui->graphicsView->setScene(scene);

Solution

  • You can use the open folder, here just snipsets :

    const QString folderPath = QFileDialog::getExistingDirectory(this, tr("Image folder"));
    if(!folderPath.isEmpty())
    {
        QDir dir(folderPath);
        QStringList filter;
        filter << QLatin1String("*.png");
        filter << QLatin1String("*.jpeg");
        filter << QLatin1String("*.jpg");
        dir.setNameFilters(filter);
        QFileInfoList filelistinfo = dir.entryInfoList();
        QStringList fileList;
        foreach (const QFileInfo &fileinfo, filelistinfo) {
            QString imageFile = fileinfo.absoluteFilePath();
            //imageFile is the image path, just put your load image code here
        }
    }