under windows i can not load images with a absolute file path in QML. Everytime i get the following error:
QML Image: Cannot open: "file//d/folder/image1.jpg"
Under Ubuntu it works perfect.
The image is set dynamic by this code:
Image {
id: img
x: 0
y: 25
width: 227
height: 230
anchors.horizontalCenter: parent.horizontalCenter
source: "file://"+path
fillMode: Image.PreserveAspectFit
}
In time i tested the following command, if i clicked on the image:
onClicked:{
console.log(path)
}
than i get the current Path: D:/folder/image1.jpg
is there a work-around for windows?
Greetings
"file//d/folder/image1.jpg" is not a valid URL. It should be "file:///d:/folder/image1.jpg".
Okay, i found a solution.
I implemented a QDeclarativeImageProvider
which handle the image-path in c++ and return a PixelMap.
If you are interested:
#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H
#include <QDeclarativeImageProvider>
class ImageProvider : public QObject, public QDeclarativeImageProvider
{
Q_OBJECT
public:
ImageProvider(QDeclarativeImageProvider::ImageType type);
~ImageProvider();
QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize);
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize);
};
#endif // IMAGEPROVIDER_H
#include "imageprovider.h"
#include <QFile>
#include <QImage>
#include <QPixmap>
#include <QDebug>
ImageProvider::ImageProvider(QDeclarativeImageProvider::ImageType type) :
QDeclarativeImageProvider(type){}
ImageProvider::~ImageProvider(){}
QImage ImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize)
{
QImage image(id);
QImage result;
if (requestedSize.isValid()) {
result = image.scaled(requestedSize, Qt::KeepAspectRatio);
} else {
result = image;
}
*size = result.size();
return result;
}
QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize)
{
QPixmap image(id);
QPixmap result;
if (requestedSize.isValid()) {
result = image.scaled(requestedSize, Qt::KeepAspectRatio);
} else {
result = image;
}
*size = result.size();
return result;
}
view->engine()->addImageProvider(QString("extern"), imageProvider);
Image {
id: img
x: 0
y: 25
width: 227
height: 230
anchors.horizontalCenter: parent.horizontalCenter
source: "image://extern/"+path
//doesn't find absolute path in windows source: "file://"+path
fillMode: Image.PreserveAspectFit
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With