Question

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

Was it helpful?

Solution 2

@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.

OTHER TIPS

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!

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top