Johnny Pauling Johnny Pauling - 2 months ago 13
C++ Question

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);
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)

I'm posting here my qrc file:

<qresource prefix="/">

it's pretty simple and I don't understand why at runtime icons are not showing up


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