Load icon from resource
-
@Leo-Z
it looks fine.
Right click the actual image and u can get right path directly. (Copy path)
It will be like ":/icons/inbox.png" -
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");
-
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. -
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.
-
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.
-
Thanks for your help
QFile::exists(":/install/x11-transparency-tools.svg") 1
QIcon(":/install/x11-transparency-tools.svg").isNull() 0I 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.