Qt: Path to file in QString - c++

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

Related

Qt, read in a local text file

I have tried to use QFile to open a text file:
I tried
QFile file("serial_deviceIP.txt");
but the file.open() returns false.
However, if I switched to a global address like:
QFile file("C:/Users/shupeng/Documents/qgroundcontrol_peidong_mod/serial_deviceIP.txt");
it works. Why? How can I solve this?
In the first instance, the path to the file cannot be found.
QFile file("serial_deviceIP.txt");
This specifies the file with a relative path, and will only work if serial_deviceIP.txt is in the current working directory, which is likely to be the directory that contains the executable of your program.
QFile file("C:/Users/shupeng/Documents/qgroundcontrol_peidong_mod/serial_deviceIP.txt");
This is referencing an absolute file path, so the file will be found
You can also use Qt's Resource System to bundle the files with your application.
Create a .qrc file in your project and add any file you wish to use/load in your application to it.
Then you can load your file as:
QFile file( ":myfiles/serial_deviceIP.txt" );
See QT Resource System for more information.
What happens is that when we are developing our code we usually keep our project source dir on mind as the reference so we don't give an absolute path, but after building the current directory will change and it will be the build directory, so our application won't find the files without a absolute path.
A possible solution is to add a Resources in our project including our project directory. So just add the following line in the project_file.pro:
RESOURCES += ./
and then use the character : before the file's name when you go to read it, like it:
QFile foo(":bar.txt")
That just work for read it but not for write. So to write is necessary specify an absolute path.

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.

Error trying to load qml from resource

I did an app loading qml from local filesystem, and it was working fine, but now I'm trying to change it to load from qml, but it never find my file.
I've looked into the web and tried some solutions, nothing worked.
My qml file is in a subdirectory of the project qml/MyProject/main.qml
Then I created a resource file and added a prefix "/" (tried with different names too), and then added my qml file to it.
Then I changed the generated qtquick2applicationviewer.cpp setMainQmlFile to: http://pastebin.com/kRJDJACW
Then I tried the following lines to load the qml from resource:
viewer.setMainQmlFile("qrc:/main.qml");
viewer.setMainQmlFile("qrc://main.qml");
viewer.setMainQmlFile("qrc:/other_prefix/main.qml");
viewer.setMainQmlFile("qrc:/qml/MyProject/main.qml");
viewer.setMainQmlFile("qrc://qml/MyProject/main.qml");
My resource file: http://pastebin.com/1sjUqk2T
Qt version: 5.2/ QtCreator 3.0
What am I doing wong?
The code you are showing looks alright, at least the following line:
viewer.setMainQmlFile("qrc:/main.qml");
The error is likely somewhere else. You have not shared the project file of yours, but the reason is probably that you do not have this file correctly in your resource.
You would need to use the following entry in your qmake file:
RESOURCES = main.qrc
Solved, thanks guys. For some reason my resource file was in "other files", not in "resources" area of Qt Creator, so I added a new one and it worked. Thanks!

How to use QResource to read a file?

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

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.