Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt qrc resource file - can't load icon

Tags:

c++

icons

qt

qt5

qicon

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

like image 807
Johnny Pauling Avatar asked Dec 27 '12 17:12

Johnny Pauling


1 Answers

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!

like image 182
Terrabits Avatar answered Sep 28 '22 12:09

Terrabits