How to use QResource to read a file? - c++

I've a text file which is added to a resource file in qt's pro file. I'd like to access this file via boost::filesystem. I've learned that I have to use QResource in order to do so, I've tried few things:
QResource resource("./Resources/setting_files/accepted_file_extensions.txt");
boost::filesystem3::ifstream fin(resource.absoluteFilePath().toStdString());
and it doesn't work, but why?

QResource is used for loading external binary resources which basically are files that are a compound of other several different files (images, documents, etc.).
The workflow is:
you create a resource file (.qrc extension) that specifies the files to be combined as a binary, using the specific Qt QRC markup tags;
you combine all the files in the resource data binary file using the command (for linux) rcc -binary myresource.qrc -o myresource.rcc;
finally you include the resource (dynamically) using a QResource instance by registering it through QResource::registerResource("/path/to/myresource.rcc"); .
This is very helpfull for importing several files using only one file. This is also very helpfull for embeded sytems.
Source: http://doc.qt.digia.com/qt/resources.html#external-binary-resources

Related

docmosis - relative link to file

I try to create a report (PDF/WORD) using docmosis.
In my report, I would like one of the fields to point to a file that resides relatively to the output-report. Is it possible? I saw the documentation that says I have to add to the template the prefix link:, e.g. <<link:linkToFile>> so docmosis understands this is a link. However, in the output report this links to a directory which does not even exist under %TMP% folder (C:\Users\ohadr\AppData\Local\Temp...).
Is there a way to add a link to file with docmosis? And how about relative file?
The <<link:abc>> directive is intended for external links which can be to files to download etc. Currently it will not reference any relative content/file.

Extract absolute file path from a QRessource loaded file

I have a file in /home/me/xmls/foo/bar.xml
and a correct qrc file containing this path
The folowing code :
QFileInfo f (":/foo/bar.xml");
cout<<f.absoluteFilePath().toAscii().data()<<endl;
outputs:
:/foo/bar.xml
when i was expecting:
/home/me/xmls/foo/bar.xml
Is there any way to recover the absolute path in the system of a QRessource loaded file ?
QResource encodes its contents into a file that gets compiled into the final binary. So, no I would expect it is not possible to get the original path at run time. As the Qt docs state: -
The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable
Also, it doesn't make sense to want the original path, as other users are unlikely to have the file at that location.
This :
QFileInfo f (":/foo/bar.xml");
means that you are going to load a file from the resource file. Basically, this is a library (static or shared), which contains resources (images, translations, etc).
That is a standard syntax to access a file in the resources, as explained in their documentation for the resources.

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

Method to store a configuration file inside executable in C++

I need to store a configuration file that can be changed once the executable has been compiled inside of an executable using C++. I assume the configuration file would need to be stored as a resource for it to be editable once the executable has been compiled.
I have no idea how I can go about storing it as a resource and how to then include it in the main section of my project while still leaving it in the resource section.
Any help will be much appreciated.
Windows "*.ini" files is one way.
But, I suggest use XML files for configuration. Most compilers have 2 or 3 libraries to load & store data from XML files.
Besides, they allow to store information in a hierarchical way, and easy to add or remove configuration options.
EDIT:
Another way are JSON files.

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.