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.
Related
I've been trying to build a card game using Qt recently, but I come across a bug which is very weird.
QFile file(":/file/02");
file.open(QIODevice::ReadWrite|QIODevice::Truncate);
The return value of the second line is false. So when I try to read or write the file, this exception is thrown.
However,the file actually exists and a qrc file in the project writes like this:
<qresource prefix="/file">
<file alias="01">data.json</file>
<file alias="02">deck.json</file>
I've also run qmake after adding this qrc file, but everything remains the same. How can I fix it? Thanks a lot.
According to the docs:
The Qt resource system is a platform-independent mechanism for storing
binary files in the application's executable. This is useful if your
application always needs a certain set of files (icons, translation
files, etc.) and you don't want to run the risk of losing the files.
As it is part of the resource, the files are read only, so if you open it with write permission it will always return false.
In short the files stored in .qrc are static.
I'm trying to load some PNG icons and some text files into my Qt program created in Qt Creator via a qrc file, but while the respective images appear on the toolbar (as they should) in the UI editor and in Linux, the buttons are blank when compiled in Windows. I even tried
QImage img(":/icons/debug.png");
qDebug() << img.isNull(); // true
I have my source files and the .pro file in basedir/src/, and the resources and .qrc file in basedir/src/res/
Is there something I'm doing wrong? If so, why does this work in Linux but not Windows?
EDIT:
qDebug() << QImageReader::supportedImageFormats(); // ("bmp", "cur", "dds", "gif", "icns", "ico", "jpeg", "jpg", "pbm", "pgm", "png", "ppm", "svg", "svgz", "tga", "tif", "tiff", "wbmp", "webp", "xbm", "xpm")
Here is my qrc file.
<RCC>
<qresource prefix="/icons">
<file>sphere-icon.png</file>
<file>project-properties.png</file>
<file>debug.png</file>
<file>qsi-icon.png</file>
</qresource>
<qresource prefix="/dictionaries">
<file>Enums.txt</file>
<file>LegacyFunctions.txt</file>
<file>MiniSphereObjects.txt</file>
</qresource>
</RCC>
Alright, I don't know why this worked, but after cleaning and rebuilding a hundred more times, deleting the .pro.user file, deleting my temporary folders (which I set in the project file to keep things more organized), removing the .qrc file reference from the project file, then restarting, suddenly everything works
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.
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
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>