Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt how to access resources

Tags:

path

resources

qt

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,

like image 262
Stefano Avatar asked Oct 12 '11 14:10

Stefano


People also ask

What is a resource file in Qt?

The Qt resource system is a platform-independent mechanism for shipping resource files in an application. Use it if your application always needs a certain set of files (like icons, translation files, images), and you don't want to use system-specific means to package and locate these resources.

What is QRC file in Qt?

The Qt Resource System is a mechanism for storing binary files in an application. The files will be embedded into the application and be acessible for the QFile class and the constructors of the QIcon and QPixmap classes taking a file name by using a special file name starting with :/ .

How do I create a QRC file?

Click the Edit Resources button (first on the left) Click the New Resource File button (first on the left) Enter a file name (e.g. resources. qrc) and click Save.


1 Answers

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.

like image 193
Luca Carlon Avatar answered Nov 07 '22 20:11

Luca Carlon