Qt: How to add resource file (such as image)

It is common that we need to add some images in our widget/form/control to create a good application layout. We can do this by inserting into the widget, the image directly or create a resource file as well. Resource file in the Qt Application is .qrc file.

In a simple shot, we use the resource file by firstly create the file, and include the files into the Qt Application project, and compiling the application.


Ho to do that in detail?




0. Create the file.

Open any plain text editor, and type the following:


<RCC>
<qresource prefix="/" >
</qresource>
</RCC>



the tagline above is necessary to get file working. after this, insert the image file name as follows:



So the complete stucture will be like this:





By this scenario, the image file location is the same as the .qrc file. Next, save the file with let say setting.qrc.



1. Open your Qt project (QtCreator). Add the following line into your .PRO files.

RESOURCES = images/setting.qrc

Here keyword RESOURCE is followed by the location of the .qrc file. Notice also that .qrc files is located under folder images (one level lower from the main folder, where .PRO file reside).

2. Build the file, so that the setting.QRC file is appeared in your project list, together with other source files.

3. Go to your user interface file (form/widget) that is .ui file. If you want to use the resource in the form window icon. You can set it via Property Pages named windowIcon. You can see two option when clicking the Property, Choose Resource and Choose File. When choosing the first option you will see Select Resource Dialog, and choose of image in the display.

4. Complete the step by compiling the files.

That's all, and have a try!

<file>filename</file>
<file>filename</file>

<RCC>
<qresource prefix="/">
<file>image1.png</file>
<file>image2.png</file>
</qresource>
</RCC>

2 comments:

  1. Anonymous8:05 PM

    Thank you so much.

    ReplyDelete
  2. Thanks for your nice words! this blog is great. I will definitely bookmark it and keep it in my portfolio of
    online document management.

    ReplyDelete