qt resources qrc not being found - c++

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.

Related

Get the container file for an embedded resource in Qt C++

Here it is shown how to get list of files stored in a .qrc Qt Resorce file?.
This solution works as expected, however the list does not show the .qrc file from which the given entry originates, as in the case where the project contains several sub-projects in the form of libraries, each with its own .qrc file.
So, the question is: Is there a way to know to which .qrc file the given resource in the obtained list belongs?
I need this information to make the library aware of which resources are available from its local .qrc, not to get all the resources, which are available globaly for the application.
Platform info: Qt 5.6.1 with MSVC2013 installed on Windows 7
No, you can't. When the qrc file compiled, the original file name won't be incorporated into compiled binary. If you want the library awares of its local resource, you should add prefix to each resource file with library identifier. For more explanation see Qt Resource System. For example
<!-- resource for mylib01 -->
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="mylib01">
<file>images/copy.png</file>
<file>images/cut.png</file>
</qresource>
</RCC>
<!-- resource for mylib02 -->
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="mylib02">
<file>images/copy.png</file>
<file>images/cut.png</file>
<file>images/new.png</file>
</qresource>
</RCC>
Each resource file then will be available as :/mylib01/copy.png, :/mylib02/copy.png, ... When listing files in the resource for a specific library, simply compare the library identifier with resource file name to check whether it is belong to the library or not.
Another option is using separate binary resource, then load it dynamically with QResource. You can access the compiled resource file name by QResource::fileName().

QT5.2 Resource files

Short yet annoying problem; I can't access anything defined within qt resource file (aka .qrc). I've followed the qt utorial for creating a widget application called TextFinder.According to it I've created all the necessary files and completed all the instructions yet I can't access the qrc contents.
Inside the project folder I have files like:
TextFinder
resources
input.txt
main.cpp
textfinder.cpp
textfinder.h
TextFinder.pro
TextFinder.pro.user
TextFinder.qrc
textfinder.ui
The contents of the qrc file is as follows:
<RCC>
<qresource prefix="/res">
<file>resources/input.txt</file>
</qresource>
</RCC>
To access the file within I opened the qrc in Editor right clicked on the file and chose copy resource path to clipboard option. This produced ":/res/resources/input.txt". So I just entered this to my function to open the file. This function looks like the following:
void TextFinder::loadTextFile()
{
QFile inputFile(":/res/resources/input.txt");
inputFile.open(QIODevice::ReadOnly);
if (inputFile.isOpen())
{
QTextStream txtStream(&inputFile);
QString contents = txtStream.readAll();
inputFile.close();
ui->textEdit->setPlainText(contents);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
else
{
throw std::runtime_error("Resource file may be wrong?");
}
}
When I run the application the runtime_error is thrown that tells me that it couldn't open the file. In the project file I have the qrc file defined as follows:
RESOURCES += \
TextFinder.qrc
What is going wrong in here? Anyone could point out what i'm doing wrong?
Regards,
Joe
According to Qt Resource System documentation:
It is also possible to specify a path prefix for all files in the .qrc file using the qresource tag's prefix attribute:
<qresource prefix="/myresources">
<file alias="cut-img.png">images/cut.png</file>
</qresource>
In this case, the file is accessible as :/myresources/cut-img.png.
So subpath is cut off when prefix is present

Qt qrc resource path doesn't work

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"

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.