TransWikia.com

When to recompile resource.qrc with pyrcc5

Geographic Information Systems Asked on August 6, 2021

In QGIS3 plugin tutorials that use the plugin builder, it says to run pyrcc5 before importing the plugin.
After what changes should pyrcc5 be run again? I have managed to modify some plugins by changing some of the Python code and the changes were there when the plugin was reloaded, without me recompiling.

I read here that pyrcc5 compiles the resource.qrc file, but I don’t really know what that file is for. The documentation mentions icons, so am I right in presuming that I would have to recompile only after changing the additional images (icons, etc.) provided to the plugin?

One Answer

As The Qt Resource System Documentation says, the main reason of using resource file is to avoid the risk of losing the files.

pyrcc5 generates all in one binary-like string which contains all resource files defined in .qrc file, and adds that string to .py file (in this case, resources.py), then you import that module (from .resources import * or from pluginname.resources import *)

When you add -for example- a new 'icon' to .qrc file or modify the icon file content, you have to run pyrcc5 again to modify resources.py.


I work on different OS and computers. I don't prefer to use .qrc file not to run pyrcc5 again or to avoid getting errors with pyrcc5. Instead, I use full path of the file using the following structure. That's personal choice.

  • Usage without resource file:

      plg_dir = os.path.dirname(__file__)
    
      # icons: folder containing my icon files
      icon_path = os.path.join(plg_dir, "icons", "zoom.png")
      # OR icon_path = plg_dir  + '/icons/zoom.png'
    
      icon = QIcon(icon_path) 
    
  • Usage with resource file:

    Before, you should genereta resource.py file using pyrcc5.

      from .resources import *
      .
      .
    
      icon_path = ':/icons/zoom.png'
      icon = QIcon(icon_path)
    

    Content of resources.qrc:

      <!DOCTYPE RCC>
      <RCC version="1.0">
          <qresource>
              <file>icons/zoom.png</file>
              ...
          </qresource>
      </RCC>
    

Correct answer by Kadir Şahbaz on August 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP