Qt qrc resource file - can't load icon
Asked Answered
W

3

15

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

Womanhater answered 27/12, 2012 at 17:5 Comment(1)
Qt resources aren't using the Windows exe resource mechanism, so it's normal they don't show up there. Please post your resource.qrc file. Also, note that you can load an image directly in a QIcon. No need to go through a QPixmap. Try simply: ui->combobox->addItem(QIcon(":/my_image.png"), "itemname");Pugnacious
P
14

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

Puzzle answered 29/12, 2012 at 18:12 Comment(0)
B
25

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!

Borrowing answered 10/9, 2013 at 23:23 Comment(1)
Thx for Copy Resource Path... this show that i have duplicated folder :/dictionaries/dictionaries/en.dicRepresentationalism
P
14

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

Puzzle answered 29/12, 2012 at 18:12 Comment(0)
C
0

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.

Cripps answered 30/9, 2016 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.