Image Gestures Example: use QFileInfo for images file names
The example keeps around image file names only and append them to the selected dir path, that works fine for file scheme files, but for Android with content scheme files, that doesn't work as good because usually the paths are returned by a provider and managing them manually like appending a file name to a directory (tree) path might not work. This patch retrieves QFileInfo objects and use the absolute file paths to open any image. Pick-to: 6.6 6.5 6.2 Fixes: QTBUG-116181 Change-Id: I9911a181d92ba0452500398cbe052b9583bd79a0 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
parent
b2f4e1e395
commit
381612f794
@ -149,15 +149,23 @@ void ImageWidget::openDirectory(const QString &path)
|
||||
this->path = path;
|
||||
QDir dir(path);
|
||||
const QStringList nameFilters{"*.jpg", "*.png"};
|
||||
files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
|
||||
files = dir.entryInfoList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
|
||||
|
||||
position = 0;
|
||||
goToImage(0);
|
||||
update();
|
||||
}
|
||||
|
||||
QImage ImageWidget::loadImage(const QString &fileName) const
|
||||
/*
|
||||
With Android's content scheme paths, it might not be possible to simply
|
||||
append a file name to the chosen directory path to be able to open the image,
|
||||
because usually paths are returned by an Android file provider and handling those
|
||||
paths manually is not guaranteed to work. For that reason, it's better to keep
|
||||
around QFileInfo objects and use absoluteFilePath().
|
||||
*/
|
||||
QImage ImageWidget::loadImage(const QFileInfo &fileInfo) const
|
||||
{
|
||||
const QString fileName = fileInfo.absoluteFilePath();
|
||||
QImageReader reader(fileName);
|
||||
reader.setAutoTransform(true);
|
||||
qCDebug(lcExample) << "loading" << QDir::toNativeSeparators(fileName) << position << '/' << files.size();
|
||||
@ -187,7 +195,7 @@ void ImageWidget::goNextImage()
|
||||
prevImage = currentImage;
|
||||
currentImage = nextImage;
|
||||
if (position+1 < files.size())
|
||||
nextImage = loadImage(path + QLatin1Char('/') + files.at(position+1));
|
||||
nextImage = loadImage(files.at(position + 1));
|
||||
else
|
||||
nextImage = QImage();
|
||||
}
|
||||
@ -204,7 +212,7 @@ void ImageWidget::goPrevImage()
|
||||
nextImage = currentImage;
|
||||
currentImage = prevImage;
|
||||
if (position > 0)
|
||||
prevImage = loadImage(path + QLatin1Char('/') + files.at(position-1));
|
||||
prevImage = loadImage(files.at(position - 1));
|
||||
else
|
||||
prevImage = QImage();
|
||||
}
|
||||
@ -234,12 +242,12 @@ void ImageWidget::goToImage(int index)
|
||||
position = index;
|
||||
|
||||
if (index > 0)
|
||||
prevImage = loadImage(path + QLatin1Char('/') + files.at(position-1));
|
||||
prevImage = loadImage(files.at(position - 1));
|
||||
else
|
||||
prevImage = QImage();
|
||||
currentImage = loadImage(path + QLatin1Char('/') + files.at(position));
|
||||
currentImage = loadImage(files.at(position));
|
||||
if (position+1 < files.size())
|
||||
nextImage = loadImage(path + QLatin1Char('/') + files.at(position+1));
|
||||
nextImage = loadImage(files.at(position + 1));
|
||||
else
|
||||
nextImage = QImage();
|
||||
update();
|
||||
|
@ -4,6 +4,7 @@
|
||||
#ifndef IMAGEWIDGET_H
|
||||
#define IMAGEWIDGET_H
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QImage>
|
||||
#include <QLoggingCategory>
|
||||
#include <QWidget>
|
||||
@ -40,14 +41,14 @@ private:
|
||||
void swipeTriggered(QSwipeGesture*);
|
||||
//! [class definition begin]
|
||||
|
||||
QImage loadImage(const QString &fileName) const;
|
||||
QImage loadImage(const QFileInfo &fileInfo) const;
|
||||
void loadImage();
|
||||
void goNextImage();
|
||||
void goPrevImage();
|
||||
void goToImage(int index);
|
||||
|
||||
QString path;
|
||||
QStringList files;
|
||||
QFileInfoList files;
|
||||
int position;
|
||||
|
||||
QImage prevImage, nextImage;
|
||||
|
Loading…
Reference in New Issue
Block a user