18

Qt Creator give the possibility to attach some resource to the project. I created in the project directory a folder called: Images. inside i have the file splash1.jpg

I created then the Resources file and attached the file as shown in the following figure: enter image description here

Which is now the correct way to access from code such a file? I tryed

QPixmap pixmap( "Images/splash1.jpg" );
QPixmap pixmap( "./Images/splash1.jpg" );

but none of them worked.

if i put just ./Images/splash1.jpg work at least if i compile by hand with qmake and make because has the correct path at runtime but no way to make it work within qt creator

Any idea??

Cheers,

1
  • 3
    Note: in Qt Designer you can use the context menu on an image in the Resource Browser and choose "Copy Path"; this will include the leading colon necessary (per the answer).
    – Phrogz
    Commented Oct 8, 2014 at 5:08

3 Answers 3

31

Qt5 resources explains all you need. You have to put the colon before the path in the source tree. You also have placed a prefix, so :/Images/Images/splash1.jpg. This will give you a path. If you need a URL, then use the qrc: scheme.

2
  • i read the documentation but i tought the semicolon was part fo the sentence instead of the path.... ahaah... thanks a lot! :)
    – Stefano
    Commented Oct 12, 2011 at 15:57
  • It works for Desktop-Apps! But when I'm working with an iOS-Device (mobile phone), reading the resource fails, with the message: file could not be found
    – peter70
    Commented Feb 14, 2018 at 7:40
4

The correct way to load with Qt resources is: :/Images/Images/splash1.jpg.

What you could also do, is assign an alias to the resource. This way you don't need the .jpg: :/Images/splash

2

You can use ":/prefix/name of your image".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.