QT5.2 Resource files - c++

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

Related

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.

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 Resources under Linux

I want to use Qt Resources file .qrc to load resources to my soft. Under Windows, it works perfectly, but under Linux (Ubuntu 12.10), it doesn't work at all.
Here is a part of my resources.qrc file :
<qresource prefix="/ressources">
<file alias="style">ressources/style.css</file>
</qresource>
When I open this file in my code I make something like that :
QFile file(":/ressources/style.css");
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "open fail";
return ;
}
open() method is unable to open that file properly.
Have you an idea ?
Thank you.
You've specified "style" as the alias, so you can only open it with:
QFile file(":/ressources/style");
However, since the prefix you specified is identical to the physical directory name, why don't you just do this instead:
<qresource>
<file>ressources/style.css</file>
</qresource>

Qt resourced files

I have a resource file
<RCC>
<qresource prefix="/">
<file>_initData</file>
<file>_LOGFILE</file>
</qresource>
</RCC>
In my code I easily access the first one but cant access the second.
QFile file(":/_initData");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Cannot open file to fill avtomatTable";
}
works just fine
QFile file(":/_LOGFILE");
if (!file.open(/*QIODevice::Truncate | */QIODevice::WriteOnly)) {
qDebug() << "Cannot open LOGFILE";
}
never works
I'm using KUbuntu. Both files are situated in this project's dir /home/template/_projects/4_Disr.
I misunderstand what is happening and got ready to believe in Cthulhu.
Any suggestions?
All data encapsulated in resource is read-only, as far as I know...
rcc compiles all resourses into binary form, usually compress them, so you can't access them in write mode.
This means that files which are in your folder taken at compile time and added to file .rcc which is used as source file for your resources. Files on your disk are just source from which resource file being assembled, your program does not uses them, just rcc.
You should create log file as standalone file, and all will work fine. Do not embed it into resource system.