Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while set up absolute image path in QML/QT (under Windows)

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

like image 349
501 - not implemented Avatar asked Apr 07 '13 18:04

501 - not implemented


2 Answers

"file//d/folder/image1.jpg" is not a valid URL. It should be "file:///d:/folder/image1.jpg".

like image 137
Fabien Avatar answered Nov 04 '22 04:11

Fabien


Okay, i found a solution. I implemented a QDeclarativeImageProvider which handle the image-path in c++ and return a PixelMap. If you are interested:

imageprovider.h

 #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

imageprovider.cpp

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

registration in mainwindow.cpp

   view->engine()->addImageProvider(QString("extern"), imageProvider);

snippet of qml-file

  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
  }
like image 23
501 - not implemented Avatar answered Nov 04 '22 05:11

501 - not implemented