Qt/C++: Icons not showing up when program is running in Kubuntu - c++

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.

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 resources qrc not being found

I´m trying to load the following file:
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/mike">
<file alias="mike.json">mike.json</file>
</qresource>
</RCC>
with
QFile file(":/mike/mike.json");
But it does not work.
So I moved
<qresource prefix="/mike">
<file alias="mike.json">mike.json</file>
</qresource>
to another .qrc file. Then it worked.
Is there some way do debug in order to check if at least the prefix is being created?
I'm loading it from
SOURCES += \
$$PWD/form.cpp
HEADERS += \
$$PWD/form.h
RESOURCES = $$PWD/mike.qrc
and .cpp and .h files are available. I do not understand why mike.qrc seems not being parsed.
Is there some way do debug in order to check if at least the prefix is being created?
Method 1: Use GammaRay, it can show your resource tree.
Method 2: In your app, use a recursive QDirIterator starting from :/, and dump the file names as found.

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 path doesn't work

I would like to show an image within a QLabel widget. The image is located in the folder ./images/ relative to the resource.qrc file and included like the this:
<RCC>
<qresource prefix="/images">
<file>image.png</file>
</qresource>
</RCC>
Now I want to show the image within a QLabel:
QPixmap pixmap( ":/images/image.png" );
label->setPixmap( pixmap );
This don't work. While in debug mode pixmap = NULL. I think the qrc path is wrong. With the absolute system path to the image c:/images/... it works fine. Any idea?
The prefix you've specified is applied to the resource path inside the app. It doesn't apply to the real path of the file. The correct resource should be:
<RCC>
<qresource prefix="/images">
<file>images/image.png</file>
</qresource>
</RCC>
And the resource path will be :/images/images/image.png.
You can also specify prefix="/" in RCC file and use ://images/image.png resource path. I think it's more convenient.
If you use an alias in your resource file giving: -
<RCC>
<qresource prefix="/images">
<file alias="image">images/image.png</file>
</qresource>
</RCC>
Then you can access your image as you are doing with: -
":/images/image.png"

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.