1

For the last couple of hours I'm trying to solve problem with displaying icon in QAction.
The problem is that icon isn't shown, only text decryption is placed on its position 'Roads'.
I've tried to debug it and all the time QPixmap is NULL, looks like it can't find file.


Here's how my code looks like:

QPixmap icon(":/road.png");
QAction *A1 = new QAction(icon,"Road...", 0);
A1->setIconVisibleInMenu(true);
A1->setVisible(true);
connect(A1, SIGNAL(triggered()), SLOT(triggeredA1()));

I've read this article. According to it I've changed my pro file by adding line:

RESOURCES = ./res/icons.qrc

In src directory I've created subdir 'res' and placed there my icons:

-src
  --res
        road.png
        load.gif
        done.gif

Here's how my icons.qrc file looks like:

<RCC>
    <qresource prefix="/">
        <file>road.png</file>
        <file>done.gif</file>
        <file>load.gif</file>
    </qresource>
</RCC>

Even after all this manipulations QPixmap still NULL. What am I doing wrong?

tema
  • 1,115
  • 14
  • 33
  • 3
    Hmmm, the file name is `roads.png`, but you refer to it as `road.png`? And put image files in the same directory where your .qrc file lives. – vahancho Jul 01 '14 at 09:59
  • @vahancho, huh it's just a typo, but thanks :) – tema Jul 01 '14 at 10:04
  • 1
    Verify, whether you can open **any** image. For example, `QPixmap icon("path_to_the_image_file"); bool b = icon.isNull();`. If you can, the problem relates to your resource file. – vahancho Jul 01 '14 at 10:08
  • @vahanaco no problem with two other files. – tema Jul 01 '14 at 10:13
  • 1
    Maybe the problem in `road.png` file, or in the image type? Try to use other file instead. Other type: gif, jpeg, etc. Try... – vahancho Jul 01 '14 at 10:16
  • the first thing you should check is the LOGS. I'm pretty sure that Qt adds to logs some information what is the problem (for example missing plugin). – Marek R Jul 01 '14 at 10:22
  • @vahancho looks like the problem really in file type. Everything works fine with jpg, but actually I don't understand why it don't want to open .png. – tema Jul 01 '14 at 10:22
  • @MarekR here's what Qt says: 'libpng warning: iCCP: Not recognizing known sRGB profile that has been edited' – tema Jul 01 '14 at 10:23
  • 1
    @tema, you may miss the png image plugin. Check the plugins/imageformats directory. Or your png file is corrupted png file, or the image of other type, but with inconsistent file name. – vahancho Jul 01 '14 at 10:23
  • @vahancho yes, you are right: 'libqgif.dylib libqico.dylib libqjpeg.dylib libqmng.dylib libqsvg.dylib libqtga.dylib libqtiff.dylib' everything I have in imageformats – tema Jul 01 '14 at 10:42

2 Answers2

1

Solution:

I'm using OS X and built Qt4 with MacPorts. For some reason libpng isn't included in build provided by MacPorts. So, the solution is to build libpng via MacPorts manually:

sudo port install libpng

and include this lib to your project by adding following line in .pro file

LIBS += -lpng

After this steps you can use .png files in your project as it was written above.

tema
  • 1,115
  • 14
  • 33
0

You can use alias and a relative "pseudo-path"

example

<qresource prefix="/images">
    <file alias="foobar">resources/images/foobar.png</file>
</qresource>

then use pixmap like so

<pixmap resource="path/to/resourcefile.qrc">:/images/foobar</pixmap>
r3wt
  • 4,642
  • 2
  • 33
  • 55