Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add image to resource file using code



  • Hey I know we can add the image to the resource file easily creating resource file but I did get any doc to add image to resource using code. I need it because I want to add it dynamically as the user told me to do. So can anyone tell me the way to add an image to the resource using code?
    Thanks in advance



  • QResource::registerResource


  • Lifetime Qt Champion

    Hi and welcome to the forums

    If you mean to alter the resource file from code,
    adding new files to it, I'm afraid it's not that easy.
    The resource file is compiled with a tool
    https://doc.qt.io/qt-5/resources.html

    So while you can have external qres files and it would be possible to generate a new one with any new files the user selected, its generally not geared towards such use case. Ressources are normally compiled into the exe and not meant to be altered.

    So what is the goal of trying do this ?

    If the goal is to have 1 big fat file with all images in, so nobody can see them, then you can do that with a DB or a QFile.
    to some degree.



  • @qtprogrammer123 Well I only want that if the user opens any image I can insert it in the resource.qrc


  • Lifetime Qt Champion

    Hi,

    As @mrjj wrote this file is used to either build resources within the executable or as external binary files. It's nothing more than an xml file that is processed by the rcc helper tool.

    What use would it be to the user to alter that file ?
    If it's to customize your UI, you should rather copy the file in a dedicated folder and use it directly if it exists or the default on from the .qrc file.



  • @SGaist means you want to say that I should put all the images of the computer in a folder and then if the user opens any file I can use that image from that folder.
    If it's what you want to say than can you please tell me how to put all image of the computer in a folder



  • When the user selects a file to add, you copy that file into a known location and refer to it from there, or you record the location of the file and use that when you need to use the image.

    QStandardPaths can help you find a suitable location for an application-wide, persistent data store (probably QStandardPaths::AppDataLocation).



  • ok @chris I will try


Log in to reply