June 22, 2011

brucewuu brucewuu
Lab Rat
49 posts

how to set the the path of images in qml file if the qml file is in resource ?

 

as we know , when the qml file is not in the resource file , we can set the path of images by relative path “relative to exe path” , or absoulte path , while when we want to hide the qml files and put them into resource file , if we still use the old way , it will try to find them
from resource file , and failed to find them . I searched it on line , people suggested use “file:///home/xx.png” to set the absolute path of images , this way works on linux , while I set it on windows like “file:///C:/test/xx.png” , it couldn’t find it , but if I run this in run edit on windows , it can find the image . How could I solve this problem ? if I want to set the relative path not using file:/// ,what should I do ?thanks for your help .

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

15 replies

June 22, 2011

leon.anavi leon.anavi
Mad Scientist
1062 posts

Please read the following thread [developer.qt.nokia.com] because I believe it discuss the same issue.

 Signature 

http://anavi.org/

June 22, 2011

spode spode
Ant Farmer
317 posts

hi. in qml you can display images with relative path so.

X = image.png (or other extention)
let’s see this example:
1) our .qml is in C:\user\nomeutente\progetto\cartellaprogetto\qml\main.qml . our X is in C:\user\nomeutente\progetto\cartellaprogetto\image\image.png
here you write in main.qml

  1.  //main.qml
  2. import QtQuick 1.0
  3. Rectangle {
  4. width: 100; height: 100
  5.  
  6. Image {
  7. source: "../image/image.png" /*where "../" indicates that the last part of the current directory is trunced, so we have C:\user\nomeutente\progetto\cartellaprogetto and then we append /image/image.png, so the absolute path is C:\user\nomeutente\progetto\cartellaprogetto/image/image.png. note that you must use "/" as separator! */
  8. }
  9. }

June 22, 2011

eirnanG eirnanG
Lab Rat
23 posts

i use this if its GIF

  1. AnimatedImage { id: bath; x: 0; y: 0;
  2. width: 240; height: 316; source: "bath"}

and for PNG or JPEG

  1.    Image {
  2.        id: image1
  3.        x: 52
  4.        y: 56
  5.        width: 100
  6.        height: 100
  7.        source: "Status.JPG"

June 23, 2011

brucewuu brucewuu
Lab Rat
49 posts

[EDIT: moved this post here from another thread, Volker]

sorry , I am afraid you guys misunderstand this question , if qml files is not embeded in the qrc files ,
there is no problem referring the qml files and images file with relative or absolute path , but if we embeded the qml files into the qrc files , while the images files is not embeded in the qrc files , like
qml refering using “qrc:/qml/myqml.qml” , images using “images/hello.png”, and images folder is under exe folder , it can’t find the image files .why I want to do it in this way , because I want to hide the qml files when I release my software , while I exposed the images files , because I want to provide skin options , customers can download new images from our website and replace the old image files , how can I solve this problem ?

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

June 23, 2011

brucewuu brucewuu
Lab Rat
49 posts

but if we embeded the qml files into the qrc files , while the images files is not embeded in the qrc files , like
qml refering using “qrc:/qml/myqml.qml” , images using “images/hello.png”, and images folder is under exe folder , it can’t find the image files .why I want to do it in this way , because I want to hide the qml files when I release my software , while I exposed the images files , because I want to provide skin options , customers can download new images from our website and replace the old image files , how can I solve this problem ?

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

June 23, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

You always can pass absolute path of your app folder (which you can retrieve in run-time) as context property to qml and concat it with your image path

June 23, 2011

Volker Volker
Robot Herder
5428 posts

If do not use an absolute path, Qt makes it relative to the surrounding QML code. And as this is in a resource, it searches for the image in the resources too.

June 23, 2011

brucewuu brucewuu
Lab Rat
49 posts

thanks , Denis and Volker , you are right , now I can solve this with Denis’s suggestion , thanks all of you guys .

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

June 23, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Volker, OP asks about using non-resourced images in resourced qml. I’m not sure it is possible with relative paths.

June 23, 2011

brucewuu brucewuu
Lab Rat
49 posts

agree with denis , maybe there is some function which can set the path of the images files , or some
protocol , like “relative:///”

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

June 23, 2011

Volker Volker
Robot Herder
5428 posts

Relative paths rely on the “parent” path to be resolved. Basically it’s an URL. You cannot use relative paths and switch the protocol (from qrc: to file:).

July 1, 2011

brucewuu brucewuu
Lab Rat
49 posts

thanks , Volker

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

July 17, 2011

jkosonen jkosonen
Lab Rat
66 posts

Did you get this to work, for me it just says “QML Image: Protocol “C” is unknown”

But it works with:

source: “file:images/image.jpg”

July 18, 2011

brucewuu brucewuu
Lab Rat
49 posts

yes , now it works , you can use

  1. QString imagePath = QDir::currentPath()+"/images/";
  2. #if defined(Q_OS_MAC)
  3. imagePath = "file://"+imagePath;
  4. #endif
  5. QDeclarativeContext::setContextProperty("imagePath",imagePath);

 Signature 

wish all the best to you from
bruce wuu
Autodesk China Inc.
SW Developer
M&E Product Developerment Group
Work 137 6411 8921
.(JavaScript must be enabled to view this email address)

November 2, 2012

misterion misterion
Lab Rat
4 posts

Also you can build this and use imagePath hack in Qt Creator and QmlViewer – https://github.com/misterion/QmlViewerDevHelper

 
  ‹‹ Problem changing the states when switching the current screen to next screen      QML and Database question ››

You must log in to post a reply. Not a member yet? Register here!