Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to add image to resource file using code

    General and Desktop
    5
    8
    582
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • U
      UG SEP last edited by

      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

      1 Reply Last reply Reply Quote 0
      • qtprogrammer123
        qtprogrammer123 last edited by

        QResource::registerResource

        Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

        U 1 Reply Last reply Reply Quote 1
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          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.

          1 Reply Last reply Reply Quote 2
          • U
            UG SEP @qtprogrammer123 last edited by

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

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              U 1 Reply Last reply Reply Quote 2
              • U
                UG SEP @SGaist last edited by

                @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

                1 Reply Last reply Reply Quote 0
                • C
                  ChrisW67 last edited by

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

                  1 Reply Last reply Reply Quote 2
                  • U
                    UG SEP last edited by

                    ok @chris I will try

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post