Nominate our 2022 Qt Champions!

Extreme confusing problem

  • Hello, guys, I am trying to create an icon in tray.
    The problem I met is rather strange.
    I added my icon.png to a .qrc file and loaded the image from that. The Image itself never shows up.
    Even if I copied the path or url given by qt creator itself, it just didn't work.
    However, when I moved my icon.png to a location on my disk say "c://" it worked.

    .pro file:
    RESOURCES += \

    .qrc file:
    <qresource prefix="/new">

    testing code:
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QGraphicsPixmapItem item(QPixmap(":/new/icon.png"));
    /* qrc:/new/icon.png won't work either */

    real code:
    QIcon icon("C:\icon.png");
    /* it works */

    "C:\icon.png" directory works fine for both testing code and real code.

    Thank you guys! I have already solved it.
    It turns out that Q_INIT_RESOURCE(***); is truly needed.
    And Unlinked problem when I added this line can be solved in following operation:

    1. Make your "***" in the parentheses is the name of your .qrc file and correctly typed
      2.Clean your project
      3.Qmake your project
      4.Run again and problem solved.

  • Hi and welcome to devnet,

    is your png file in the same directory of the .qrc??

  • @mcosta
    Yes.And I tried creating a folder in the same directory of .qrc file and move png to that folder. It didn't work

  • Have you tried to use the resource icon in your real code??

    To show an Item in a GraphicsView you have to set correctly GraphicsScene rect and the view

    This code


    works well for me using the resource

        <qresource prefix="/images">

  • Maybe the Q_INIT_RESOURCE(<qrc file name>) is missing from main?

    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        MainWindow mainWin;;
        return app.exec();

  • @Rondog Q_INIT_RESOURCE is not required if you add the resource file in the .pro file

  • @Rondog Thank you Randog, But it is not the problem.
    I think that this line might be unnecessary. But you brought this up, I have another question for this line.
    I have tried this even before I asked here. And when I add this line to my main.cpp, It told me " LNK2019 unresolved symbol".
    There is an example in Qt document using this line, and I had the exact same libraries included.


    1. resource file has been included in .pro file.
    2. Q_INIT_RESOURCE(Ima); /* name of my qrc file is Ima.qrc now*/

  • @mcosta Yes, I have. It doesn't work.

  • @PandaBoy could you attach your project? (the whole project directory)

  • The unresolved linker error will occour if the name inside the macro Q_INIT_RESOURCE cannot be found.

    I was not aware that this wasn't required, I tried it and sure enough it worked without it. It is interesting that when you did add it you ended up with a link error which means it can't be loaded (I have always added this line inside of main and get this error when I don't set the name properly).

    From a sample program I have the following (that does work):

    // qrc file =====================
    <!DOCTYPE RCC><RCC version="1.0">
    	<qresource prefix="/gui">
    // pro file =====================
    // note:  I put all the resources in a folder called 'res'
    RESOURCES += res/autocapture.qrc
    // usage example =====================

    A couple of observations that might be helpful.

    The resources line in the .PRO file ...

    RESOURCES += \

    ... might be a problem. Why not this instead:

    RESOURCES += Ima.qrc

    The .qrc file is missing the first XML identification line (i.e. '<!DOCTYPE RCC>' ). Maybe the file is not recognized as a resource file without this line?

  • Hi,

    <!DOCTYPE RCC> is not needed.

    You have to add Q_INIT_RESOURCE when you want to load resources not embedded in your application.

    I'm not sure to understand; the simple icon example works or not??
    Your app with QGraphicsScene??

Log in to reply