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

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

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.

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

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!

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.

Display image and resources problem

I am using netbeans 6.9 with QT on ubuntu.
I want to display an image in my release build , but i got this error when compiling:
/usr/bin/rcc: File does not exist 'res.qrc'
This is my res.qrc file
<RCC>
<qresource prefix="image">
<file>icon.gif</file>
</qresource>
</RCC>
In my qt-Release.pro i added:
RESOURCES = res.qrc
My application runs but obviously my image won't be displayed.
res.qrc and the image is in my main project folder(where the source files are located).
Whats wrong? Am i missing anything? I studied the guide from
here
You set <qresource prefix="image"> but then note that
the image is in my main project folder(where the source files are located)
That's one problem. It would need to be in the image folder.