Qt Resources under Linux - c++

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>

Related

Why are Qt resources being loaded in Linux but not Windows?

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

QT5.2 Resource files

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

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/C++: Icons not showing up when program is running in Kubuntu

I did everything what I need to do:
added an icon to resource file
initialized resource in main file: Q_INIT_RESOURCE(images);
set icon: ui->action_New->setIcon(QIcon(":/images/about_me.png"));
but still cant see an image when I compile and run my application (I use Kubuntu 12.04 and Qt 4.8.1). Here's a little test project: http://www27.zippyshare.com/v/45362924/file.html. What's wrong and how to fix it?
Pay attention to the alias bit in your .qrc file.
I prefer to set <qresource prefix="/"> to keep it simple.
<RCC>
<qresource prefix="/">
<file alias="about me">images/about_me.png</file>
<file alias="BSD License">otherfiles/LICENSE.txt</file>
...
</qresource>
</RCC>
This way you don't need to bother remembering the full path to use a resource
ui->action_New->setIcon(QIcon(":/about me"));
this->setWindowIcon(QIcon(":/about me"));
...
QFile lfile (":/BSD License");
if(lfile.open(QIODevice::ReadOnly){
...
}
Note: I'm not suggesting that using spaces in the aliases is a good or bad idea, but it certainly works.

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.