Nominate our 2022 Qt Champions!

Load icon from resource

  • Hello everyone, I meet a problem which is quite weird.
    I set up a treeview and add some rows in it. Then I want to add icons for them. It goes well when I add them by

    //myicon = new QIcon("C:/Users/yz001/Documents/Qt/Learning Qt/QtToDoListV1_0/inbox.png");

    However, when I add the picture into my resource and add them by

    myicon = new QIcon(":/inbox.png");

    They just don't show up.
    Could you please me how to make it via resource?
    The following is part of my code:

        ui->treeView->setModel(taskModel);  //set model
        ui->treeView->header()->hide();     //hide header
        ui->treeView->setRootIsDecorated(false);    //hide the root
        //myicon = new QIcon("C:/Users/yz001/Documents/Qt/Learning Qt/QtToDoListV1_0/inbox.png");
        myicon = new QIcon(":/icons/inbox.png");
        QStandardItem* item_inbox = new QStandardItem(QString("Inbox"));
        QStandardItem* item_starred = new QStandardItem(QString("Starred"));
        QStandardItem* item_today = new QStandardItem(QString("Today"));
        QStandardItem* item_week = new QStandardItem(QString("Week"));

  • Lifetime Qt Champion

    You must add a qres file to you project
    Right click this and add picture to it.

    Then you can use
    new QIcon(":/inbox.png"); syntax
    ":/" is syntax for images/files in a res file.
    use the first pat on how to add the qres file.
    Stop at
    "Finish. Then, the Designer will provide us with resource editor."
    Then right click this new file (Myres) and add image to it.

  • @mrjj
    I think I did add a qres file like this.
    Is this enough to use the resource?

  • Lifetime Qt Champion

    it looks fine.
    Right click the actual image and u can get right path directly. (Copy path)
    It will be like ":/icons/inbox.png"

  • @mrjj
    I did that too actually. It's kind of frustrating. Code and application

  • Moderators

    The path seems fine. Make sure you re-run qmake (Build->run qmake) after you change your resources.

    On another note you've created a memory leak. Don't create QIcons on the heap, there's no point. This is enough:

    QStandardItem* item_inbox = new QStandardItem(QIcon(":/icons/inbox.png"), "Inbox");

  • @Chris-Kawa
    Thank you. It works after run qmake. But could you please tell me why I need to run qmake after change my resources?In fact, this is my first time to run qmake before build a app. I still don't know the role of the qmake.

  • Moderators

    qmake is the tool that generates makefiles that your build tool uses to compile the project. It also runs moc, uic, ad rcc tools to generate QObject metadata, transform .ui into .h files and transform the .qrc file into .cpp file (open the build directory to see the generated files). In most cases it runs automatically (like when you add a file to your project or edit a .ui file), but it's spotty in some situations and you need to run it manually. A good rule of thumb is to run it manually whenever you change contents of the .pro file or modify resources. In general a majority of "weird behavior" like program not seeing some changes you made in code/config can be fixed be re-running qmake.

  • @Chris-Kawa
    Thank you. This information is really helpful for me:)

  • And how do you debug when it does not work?

    I built a project that appears to follow these steps but does not have icon.

  • @michals said in Load icon from resource:

    And how do you debug when it does not work?

    • Check QFile::exists(":/icons/inbox.png")?
    • Check QIcon(":/icons/inbox.png").isNull()?
    • Check what QImage(":/icons/inbox.png") actually looks like?

  • Thanks for your help

    QFile::exists(":/install/x11-transparency-tools.svg") 1
    QIcon(":/install/x11-transparency-tools.svg").isNull() 0

    I can load the icon fine when I specify a file path but for some reason it was failing when using the resource. Now I returned to loading from resource and it works with the same code that was there initially. Not sure what the problem was. And that's exactly the frustrating part - the documentation is not very clear on this resource loading and when it breaks you have no idea why.

Log in to reply