Qt qrc resource file - can't load icon

35,447

Solution 1

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

Solution 2

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!

Share:
35,447

Related videos on Youtube

Johnny Pauling
Author by

Johnny Pauling

Updated on July 09, 2022

Comments

  • Johnny Pauling
    Johnny Pauling almost 2 years

    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

    • Nikos C.
      Nikos C. over 11 years
      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");
  • Gelldur
    Gelldur over 7 years
    Thx for Copy Resource Path... this show that i have duplicated folder :/dictionaries/dictionaries/en.dic