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. Adding image files to .qrc dynamically
Forum Updated to NodeBB v4.3 + New Features

Adding image files to .qrc dynamically

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 4.3k Views 1 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.
  • D Offline
    D Offline
    Dcqt
    wrote on last edited by
    #1

    Hi ,

    Is there any way to add images to .qrc file dynamically from the program itself.
    i would like to place all my images in a particular folder and when i compile my program the newly copied images should be added automatically.

    Regards,
    Dcqt.

    1 Reply Last reply
    0
    • francescmmF Offline
      francescmmF Offline
      francescmm
      wrote on last edited by
      #2

      I think that is not possible since the .qrc file is compiled and qrc_resourcesNameFile.cpp generated. This is the fail then added to the exec.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        I'm not sure I am following you.

        Would you like that i.e. Qt creator to automatically append newly added images from a folder to a given qrc before building ?

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dcqt
          wrote on last edited by
          #4

          Ok , if that is not possible i trying to read the contents of .qrc file using below approach.

          my app is supposed to run on multiple platforms , will the following code portable or is there any stanadard way of extracting required files from .qrc.

          @
          QFile file("/home/wip/lays/images_QRC.qrc");
          file.open(QIODevice::ReadOnly|QIODevice::Text);

          QTextStream in(&file);
          QString line = in.readLine();
          while(!in.atEnd()){
              if(line.contains("jpeg")||line.contains("png")){ // filters only images 
                  qDebug()<<"line 1 "<<line;
                  QStringList line1 = line.split("<file>"); //removes "         <file>"
                  line = line1[1];
                  qDebug()<<"line 2 "<< line1[1];
                  QStringList line2 =line.split("</file>");//removes  </file>
                  line = line2[0];
                  qDebug()<<"line 3 "<<line;
              }
              line = in.readLine();
          }
          

          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Seamus Boyle
            wrote on last edited by
            #5

            You'll have more chance of success reading the qrc file as xml. Your code assumes that tags and text are all on one line, which may not be the case.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dcqt
              wrote on last edited by
              #6

              Hi,
              is there any standard way , to extract files from qrc?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                There are no files to extract from the qrc file.

                It's just an xml list of files on your system that rcc will transform to be compiled in your application, see the "rcc":http://qt-project.org/doc/qt-4.8/rcc.html doc

                For more information there is the "resources":http://qt-project.org/doc/qt-4.8/resources.html documentation

                Hope it helps

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

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DrumM8
                  wrote on last edited by
                  #8

                  There is a much better way to generate to do this during the build process:

                  Make a template for Jinja (http://jinja.pocoo.org/), which is a python template interpreter:
                  resources.qrc template:

                  <!DOCTYPE RCC>
                  <RCC version="1.0">
                      {% for key, value in input_files.items() -%}
                      <qresource prefix="{{ key }}">
                      {%- for file in value %}
                          <file>{{ file }}</file>
                      {%- endfor %}
                      </qresource>
                      {% endfor -%}
                  </RCC>
                  

                  Main code for your Python script:

                  #!/usr/bin/env python
                  from jinja2 import Environment, FileSystemLoader, select_autoescape
                  import os
                  
                  from jinja2 import Environment, FileSystemLoader, select_autoescape
                  env = Environment(loader=FileSystemLoader(options.templates),
                                    autoescape=select_autoescape(['qrc']))
                  template = env.get_template('resources.qrc')
                  
                  input_files = []
                  for root, dirs, files in os.walk(path_with_input_files):
                      for file in files:
                          input_files += file
                  
                  with open(output.qrc, "w") as resource_file:
                  		resource_file.write(template.render(input_files=input_files))
                  

                  This will generate a new output.qrc file with the files from path_with_input_files! ;-)

                  1 Reply Last reply
                  1

                  • Login

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