Get the container file for an embedded resource in Qt C++ - 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().

Related

Why does giving ":/Icons/controller.png" to a constructor work (is that valid relative filepath syntax)?

The details of the situation don't matter, but in particular I'm making a QIcon object (part of the Qt library) out of a .png file. The constructor for this takes a string that should be a filepath
I find that, instead of using an absolute path, I can simply give something like ":/Icons/controller.png" to the constructor and the icon is still correctly retrieved and displayed.
My project is huge so I won't write out the hierarchy and where the file is relative to everything else, but they're at 3 degrees of separation at least. Because of this I think there's some sort of file searching mechanic that I don't know of. Can anyone guess what's going on here?
:/ paths are paths in the Qt Resource System.
You have a QRC file that is compiled and bundled with your executable. It references the images.
E.g. your qrc file contains this:
<RCC version="1.0">
<qresource>
<file>Icons/controller.png</file>
</qresource>
</RCC>
This is then compiled in your build process with the RCC compiler. This file is then linked with your exe.
The default resources are automatically registered, but you can also compile resources into external RCC bundles, and load them at runtime using QResource::registerResource("/path/to/myresource.rcc");

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.

qt open excel name passing error

now i am making one program which reads two excel sheets and makes it one.
and all are fine. but i have one problem
that is
QAxWidget *excel=new QAxWidget("Excel.Application", this);
excel->dynamicCall("SetVisible", true);
QAxObject *workbooks=excel->querySubObject("WorkBooks");
workbooks->dynamicCall("Open(const QString&)", QString(":/temp/temp.xls"));
QAxObject *workbook=excel->querySubObject("ActiveWorkBook");
QAxObject *worksheets=workbook->querySubObject("WorkSheets");
my qrc file
<RCC>
<qresource prefix="/">
<file>temp/temp.xls</file>
</qresource>
</RCC>
workbooks->dynamicCall("Open(const QString&)", QString(":/temp/temp.xls"));
this part occurs error
i inserted one excel template file into my resource.qrc
and i tried to open but it didn't work.
if i passed absolute path of the file, then work. but if i passed relative path of the file, don't work
how can i solve this??
please let me know..
thanks
You can put you excel file in some directory inside the project like docs or something.
Set your current directory QDir::setCurrent(QCoreApplication::applicationDirPath())
You can access the files inside doc folder like "docs/anyfile.xls"
If you put the xls file inside resources it will bundled inside executable and won't be accessible by Microsoft Office COM components.
Put you excel file in some directory inside the project e.g. templates.
So you structure will be
ProjectMainDirectory/
+ - MyResources.qrc
+ - template/
+ - temp.xls
Add the file as a resource. The resource file will be like :
<RCC>
<qresource prefix="/Files">
<file>template/temp.xls</file>
</qresource>
</RCC>
Now try and access the file as ":/Files/template/temp.xls. You should be able to access the file.
When you compile the compiler will read the resource file and package the resource file in the exe itself. You will not need to package the xls file along with the exe separately.
Hope that solves the problem. If any problems please leave a comment.

Qt: Path to file in QString

I have problem with storing a path to file in Windows in a QString. I'm using Qt with C++.
QString resourcePath = ":/images/frog.bmp";
if( ! QFile::exists(resourcePath) )
{
qDebug("*** Error - Resource path not found : %s", resourcePath.data());
}
This code results with this:
*** Error - Resource path not found : :
So I can see that resourcePath.data()) contains just ":". I assumed that the problem is with slashes, so I tried changing "/" with "\" but the result is the same.
But if I write:
QString resourcePath = "C:\\Users\\Boris\\Desktop\\Frogger3\\images\\frog.bmp";
everything works just fine. What am I missing? Is there a reason why colon cant be the first sign in QString? How should I write path to the file in the same folder as the code?
Thanks in advance!
The style of resource path you are using is implying that the file frog.bmp is in a resource file. So either you need to resolve the path of the bmp file at run-time, or you need to add a resource file to your project.
If you use the UI designer the concept of resource files is handled automatically, but if you want to access resources through code there are a few things you need to do.
First create a resource file. In visual studios (with the visual studios add-in) there is a wizard to do this. Essentially it is just an xml file with the extension .qrc looking something like this:
<RCC>
<qresource prefix="/images">
<file>frogger.bmp</file>
</qresource>
</RCC>
Now this file has to be processed during the build. If you have used .ui files, it is similar. There is a tool called "rcc.exe" that takes the qrc file as an input and generates a .cpp file that needs to be compiled and linked with your project.
If you are using visual studios and have the Qt Visual Studios Plugin, this should all be handled for you when you add the qrc file to the project.
If you are using QMake then your pri file should contain a "RESOURCES" section where you need to list your qrc file something like:
RESOURCES += yourqrcfile.qrc
Now, once that is done. You can access your resources in code. Your call to QFile::exists should resolve the file name.
In the case where you put your resources in a static or shared library, you will need to add the following line to your class to ensure that the resource file is loaded.
Q_INIT_RESOURCE(yourqrcfile); // do not include the extension, just the name of the file.
Here are a few links that explain things in more detail:
Creating a resource file in Qt Creator
Explaining how resource files work

Qt Resource file utilization

Here i am describing the problem that i faced with Qt resource .rcc file.
first, When i created the .qrc file in my project it will fit all the resources that is added in the qrc, in to executable binary file.
second, rcc file in Qt used for well and optimize resource utilization and when i create it in my project it is still including all the resources (added in .qrc file) into the executable binary file even rcc file already contains all the resources so, my question is why to use this rcc even if resources are included in executable binary file. Why to include redundancy in project??
it may possible i misinterpret something or i am not aware of some points, please correct me if i wrong.
It's too late for answer, but may be helps anybody.
I've expected similar problem, and used next solution:
if you use QtCreator, just wrap your RESOURCES += xxx with config condition in .pro file, like that:
!realbuild {
RESOURCES += xxx.qrc
}
and set CONFIG+=realbuild to qmake params. What does it given? You may edit your forms with QtCreator's designer, and use resource directly from editor, but it will not be compiled into your target file, resources must be loaded in run-time using QResource::registerResource(). Use can build resources manually, using direct call to rcc tool, or write a simple script, and call it using QMAKE_POST_LINK variable.
Now question is - how to reload resources in run-time?...
There are two options for Qt resources:
include the .qrc in your .pro file with
RESOURCES = myapp.qrc
create an external binary resource file with rcc, then register it at runtime with
QResource::registerResource("/path/to/myresource.rcc");
Don't do both. i.e. if you previously had the .qrc directly included in your .pro, and now want to include it dynamically, remove the RESOURCES line from the project file and do a clean build. External binary resources are not included in your executable if you don't list them in the RESOURCES setting of your project.