Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt qrc resource path doesn't work

Tags:

c++

qt

qpixmap

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?

like image 244
Xear Avatar asked Jun 14 '13 07:06

Xear


People also ask

How do I use QRC in PyQt5?

Using a QRC file qrc file in your application you first need to compile it to Python. PyQt5 comes with a command line tool to do this, which takes a . qrc file as input and outputs a Python file containing the compiled data. This can then be imported into your app as for any other Python file or module.

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 :/ .


2 Answers

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.

like image 101
Pavel Strakhov Avatar answered Sep 21 '22 17:09

Pavel Strakhov


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"

like image 41
TheDarkKnight Avatar answered Sep 24 '22 17:09

TheDarkKnight