Qt qrc resource path doesn't work
Asked Answered
L

2

8

I would like to show an image within a QLabel widget. The image is located in the folder ./images/ relative to the resource.qrc file and included like the this:

<RCC>
    <qresource prefix="/images">
        <file>image.png</file>
    </qresource>
</RCC>

Now I want to show the image within a QLabel:

QPixmap pixmap( ":/images/image.png" );
label->setPixmap( pixmap );

This don't work. While in debug mode pixmap = NULL. I think the qrc path is wrong. With the absolute system path to the image c:/images/... it works fine. Any idea?

Larock answered 14/6, 2013 at 7:30 Comment(3)
have you initialize your resource?Neldanelia
I think so. in my .pro file it's mentioned as "RESOURCES = resource.qrc"Larock
you need something like 'Q_INIT_RESOURCE(resources);' in your programNeldanelia
T
10

The prefix you've specified is applied to the resource path inside the app. It doesn't apply to the real path of the file. The correct resource should be:

<RCC>
    <qresource prefix="/images">
        <file>images/image.png</file>
    </qresource>
</RCC>

And the resource path will be :/images/images/image.png.

You can also specify prefix="/" in RCC file and use ://images/image.png resource path. I think it's more convenient.

Talbot answered 14/6, 2013 at 7:38 Comment(2)
ok, everything is fine. I need to execute qMake after the changes in .pro file. your description was absolutely correct. thanksLarock
QMake didn't quite do it for me, but a restart of QtCreator did.Incorporable
C
6

If you use an alias in your resource file giving: -

<RCC>
<qresource prefix="/images">
    <file alias="image">images/image.png</file>
</qresource>
</RCC>

Then you can access your image as you are doing with: -

":/images/image.png"

Cobwebby answered 14/6, 2013 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.