3

I'm building qgis plugin and i use Qt creator to creat a form. I want insert a image and i use a label to do this. In the field pixmap i choose a file image.png.
I save and in QGIS i reload a plugin and the image don't appear. I edit the file xxxx.ui and i see this

   <property name="pixmap">
    <pixmap>image.png</pixmap>
   </property>

but if i do that

   <property name="pixmap">
    <pixmap>C:\Users\...\.qgis2\python\plugins\...\image.png</pixmap>
   </property>

the image appear.But if i use in another PC the image disappear again because the Path is different. Someone can help me ?

2 Answers 2

6

It's a while ago I did this this the last time but as far as I remember you can use a resource-file http://doc.qt.io/qt-4.8/resources.html#resource-collection-files-op-op-qrc , add your image there, choose the image from this resource in qt-designer and compile the resources.

Edit1: Screenshot added: Assign Image from ressources, not from file:

enter image description here


Edit2: I just gave it a try again and this workflow was successful:

  1. put your image into your plugin-folder and add it to your ressources file: enter image description here

  2. compile your resources file:

    pyrcc4 -o resources.py resources.qrc

  3. open the ui-file in QT-Designer and edit resources --> open resource file enter image description here

  4. assign logo from resources to label, not from file!

enter image description here

So your code points to your resource file:

   <property name="pixmap">
    <pixmap>:/plugins/stackexchange/logo.png</pixmap>
   </property>

If you are using plugin-builder you should use the latest version. Otherwise an error could occur ("ImportError: No module named resources_rc") ( https://github.com/g-sherman/Qgis-Plugin-Builder/issues/34 ) that would have to be fixed manually by changing

 <resources>
  <include location="resources.qrc"/>
 </resources>

back to

  <resources/>

(solution found here: https://gis.stackexchange.com/a/155599/45041).

If you use the newest plugin-builder you won't have to compile the ui again, otherwise you will have to do this step, too.

There you are:

enter image description here

PS: I'm not sure if this is the most 'elegant' way to get the image into your plugin, as I'm not woking that much with python/PyQt yet ;)

0
2

If it does not work from within the *.ui you can still assign the pixmap from your corresponding *.py file:

self.label1.setPixmap(QtGui.QPixmap(":/plugins/PluginName/image.png"))

If the image is not found you can try the resolve function by Nathan W to find the image in the plugin structure.

img_path = resolve('image.png')
self.label1.setPixmap(QtGui.QPixmap(logo_path))

Not the answer you're looking for? Browse other questions tagged or ask your own question.