Qt qrc resource path doesn't work - c++

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?

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.

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"

Related

qt resources qrc not being found

I´m trying to load the following file:
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/mike">
<file alias="mike.json">mike.json</file>
</qresource>
</RCC>
with
QFile file(":/mike/mike.json");
But it does not work.
So I moved
<qresource prefix="/mike">
<file alias="mike.json">mike.json</file>
</qresource>
to another .qrc file. Then it worked.
Is there some way do debug in order to check if at least the prefix is being created?
I'm loading it from
SOURCES += \
$$PWD/form.cpp
HEADERS += \
$$PWD/form.h
RESOURCES = $$PWD/mike.qrc
and .cpp and .h files are available. I do not understand why mike.qrc seems not being parsed.
Is there some way do debug in order to check if at least the prefix is being created?
Method 1: Use GammaRay, it can show your resource tree.
Method 2: In your app, use a recursive QDirIterator starting from :/, and dump the file names as found.

Adding files to Qrc ressources

I have added a jpg file to the QRC ressources to not drag everytime the full path of the image file.
However, it's not displayed when I am starting the user interface where it's supposed to be loaded!!
I added to it an alias. It's not working. But when I write the name of the file included in the project solution, it's well displayed.
PS: I want to organize my project and put the file in QRC ressources
This is how did I do:
QPixmap img(":/projet/image.jpg"); // It's not loaded
QPixmap img("image.jpg"); // It's OK
Please check your .qrc file first.
You can check your file with another editor like notepad++, vim , joe etc.
Does it look similar to this example:
<RCC>
<qresource prefix="/project">
<file>FOLDER/image.jpg</file>
</qresource>
</RCC>
If it does you can access your image through:
QPixmap example(":/project/FOLDER/image.jpg");
Good luck!

How to access files specified in a .qrc file from the c++ code?

I created a .qrc file in Qt 5.0.1:
<RCC>
<qresource>
<file>105.ico</file>
</qresource>
</RCC>
and I edited my .pro file:
RESOURCES += \
Icons.qrc
when I use the code below in my class constructor icon doesn't appear
this->setWindowIcon(QIcon(":105.ico"));
but when I give a local file address instead of ":105.ico" icon shows up. what is the problem?
It should be:
this->setWindowIcon(QIcon(":/105.ico"));
(Note the slash)

Qt/C++: Icons not showing up when program is running in Kubuntu

I did everything what I need to do:
added an icon to resource file
initialized resource in main file: Q_INIT_RESOURCE(images);
set icon: ui->action_New->setIcon(QIcon(":/images/about_me.png"));
but still cant see an image when I compile and run my application (I use Kubuntu 12.04 and Qt 4.8.1). Here's a little test project: http://www27.zippyshare.com/v/45362924/file.html. What's wrong and how to fix it?
Pay attention to the alias bit in your .qrc file.
I prefer to set <qresource prefix="/"> to keep it simple.
<RCC>
<qresource prefix="/">
<file alias="about me">images/about_me.png</file>
<file alias="BSD License">otherfiles/LICENSE.txt</file>
...
</qresource>
</RCC>
This way you don't need to bother remembering the full path to use a resource
ui->action_New->setIcon(QIcon(":/about me"));
this->setWindowIcon(QIcon(":/about me"));
...
QFile lfile (":/BSD License");
if(lfile.open(QIODevice::ReadOnly){
...
}
Note: I'm not suggesting that using spaces in the aliases is a good or bad idea, but it certainly works.

Display image and resources problem

I am using netbeans 6.9 with QT on ubuntu.
I want to display an image in my release build , but i got this error when compiling:
/usr/bin/rcc: File does not exist 'res.qrc'
This is my res.qrc file
<RCC>
<qresource prefix="image">
<file>icon.gif</file>
</qresource>
</RCC>
In my qt-Release.pro i added:
RESOURCES = res.qrc
My application runs but obviously my image won't be displayed.
res.qrc and the image is in my main project folder(where the source files are located).
Whats wrong? Am i missing anything? I studied the guide from
here
You set <qresource prefix="image"> but then note that
the image is in my main project folder(where the source files are located)
That's one problem. It would need to be in the image folder.