Display image and resources problem - c++

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.

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

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.

QT Resource File - Images Not Showing When Running

I just upgraded to QT Creator 5.3 and created a brand new QT Widgets Application project and am using the Microsoft VC++ compiler. All I have is a resource file with "logo.png" added (which opens in QT if I double click it), and a label that I am trying to set the background image for. The problem is that no matter what I do I cannot get the image to show up when the program is running. The only way I can get it to show the image is by not using the resource file and instead map directly to the file (ex. "c:/blah/blah/logo.png")
Also, if I set the background image in the UI designer, the background shows up in the IDE but NOT when I run the program. I have tried probably 20+ variations of the code, including resource file aliases as well as adding the files to the project directly, and nothing seems to work.
I'm not sure if there is a step I am missing, or if perhaps there is something that I have to do to get the project to compile the image.
Resources.qrc
<RCC>
<qresource prefix="/">
<file>logo.png</file>
</qresource>
</RCC>
loginform.cpp
QPixmap pixmap = QPixmap (":/logo.png");
ui->label->setPixmap( QPixmap(pixmap));
Project.pro
OTHER_FILES += \
logo.png
RESOURCES += \
Resources.qrc
After countless hours of trial, error, and mostly frustration, I have been able to resolve this issue by simply running qmake.
To do this I just right clicked on the project and clicked "Run qmake" and suddenly the image errors (as well as a few others) were resolved!

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 qrc resource file - can't load icon

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:
RESOURCES = resource.qrc
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
The problem is: the icon won't show up!
The following works:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)
Update:
I'm posting here my qrc file:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
it's pretty simple and I don't understand why at runtime icons are not showing up
I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.
Qt Creator is awesome.
Hope this helps!
#Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
In your code, you have:
QPixmap pixmap = QPixmap ("://my_image.png");
but, according to the documentation, it should be
QPixmap pixmap = QPixmap (":/my_image.png");
or you can give aliases to your resources, and use those instead.
Issue is solved is use rcc.exe
C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp
During compilation you should have images in the path.
Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.