Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add image to resource file using code
Forum Updated to NodeBB v4.3 + New Features

How to add image to resource file using code

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 2.3k Views 3 Watching
  • 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 Offline
    U Offline
    UG SEP
    wrote on 12 Jun 2021, 07:14 last edited by
    #1

    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
    0
    • Q Offline
      Q Offline
      qtprogrammer123
      wrote on 12 Jun 2021, 09:11 last edited by
      #2

      QResource::registerResource

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

      U 1 Reply Last reply 12 Jun 2021, 12:11
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 12 Jun 2021, 09:45 last edited by
        #3

        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
        2
        • Q qtprogrammer123
          12 Jun 2021, 09:11

          QResource::registerResource

          U Offline
          U Offline
          UG SEP
          wrote on 12 Jun 2021, 12:11 last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 12 Jun 2021, 18:34 last edited by
            #5

            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 13 Jun 2021, 03:47
            2
            • S SGaist
              12 Jun 2021, 18:34

              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.

              U Offline
              U Offline
              UG SEP
              wrote on 13 Jun 2021, 03:47 last edited by
              #6

              @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
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on 13 Jun 2021, 05:57 last edited by
                #7

                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
                2
                • U Offline
                  U Offline
                  UG SEP
                  wrote on 13 Jun 2021, 10:36 last edited by
                  #8

                  ok @chris I will try

                  1 Reply Last reply
                  0

                  1/8

                  12 Jun 2021, 07:14

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved