使用 Qt 从文件夹加载图像

Load images from folder with Qt

我是 QT 的新手,我想显示很多图像,并能够从一个文件夹中加载它们,我只有一个图像的代码,它工作正常但希望得到相同的结果很多图片,这是我的代码:

 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);

您可以使用打开的文件夹,这里只是片段集:

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
    }
}