June 23, 2011

BlackWiCKED BlackWiCKED
Lab Rat
4 posts

[Solved] QML files and JPG image in QRC

 

I ran into a problem, when I tried to create a Windows installer from my application. Because I have several qml and image files, I put them into one resource file. I will use the Hello world program to demonstrate:

main.cpp:

  1. #include <QDeclarativeEngine>
  2. #include <QDeclarativeContext>
  3. #include <QtGui/QApplication>
  4. #include "qmlapplicationviewer.h"
  5.  
  6. #include "Test.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication app(argc, argv);
  11.  
  12.     QmlApplicationViewer viewer;
  13.  
  14.     Test* test = new Test();
  15.  
  16.     viewer.engine()->rootContext()->setContextObject(test);
  17.  
  18.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  19.     viewer.setSource(QUrl("qrc:///qml/main.qml"));
  20.     viewer.showExpanded();
  21.  
  22.     return app.exec();
  23. }

main.qml:
  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     width: 360
  5.     height: 360
  6.     Image { source: "../gfx/Desert.jpg"; anchors.fill: parent; }
  7.     Text {
  8.         text: "Hello World"
  9.         anchors.centerIn: parent
  10.         font.pixelSize: 18
  11.     }
  12.     MouseArea {
  13.         anchors.fill: parent
  14.         onClicked: {
  15.             Qt.quit();
  16.         }
  17.     }
  18. }

Test.qrc:
  1. <RCC>
  2.     <qresource prefix="/">
  3.         <file>qml/main.qml</file>
  4.         <file>gfx/Desert.jpg</file>
  5.     </qresource>
  6. </RCC>

When I compile from QT Creator and run everything works fine. I see a window with background image with the “Hello world” text.

BUT: When I copy the created executable (with necessary dlls) to another machine, it won’t load the image. I see only the “hello world” text with white background. What it means? It means that the compiled program finds the qml but not the image from the same QRC resource file.

PLEASE NOTE:
I tried almost every combination with Image source location, I mean: placed into same folder the QML and the PNG files
source: “Desert.jpg”
tried to load directly from QRC:
source: “qrc:/gfx/Desert.jpg”, “qrc:///gfx/Desert.jpg”
etc…

In most cases it worked from the compiled folder, but never on the other machine. Checking the size of the executable file I suppose that it contains the image, why it doesn’t loads then? Please help, did I missed something?

[edit: code highlighted / Denis Kormalev]

3 replies

June 23, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Jpeg plugin is missing.

June 23, 2011

BlackWiCKED BlackWiCKED
Lab Rat
4 posts

That is it!

I just copied imageformats folder from qt/plugins to my application and it worked.

Thank you very much!

June 23, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

You are welcome.
Don’t forget to mark thread as [solved].

 
  ‹‹ [solved]Support multi platform issue - Windows and Ubuntu      Problem with transform of pointer ››

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