0

I created a custom form with QtDesigner. For the image in the form I used a QLabel and added the path of the picture to the "pixmap" attribute. The path is

C:\Users\Dennis\Desktop\Formulare\Baum_grün.png

The object name of the lable is called "Bild"

enter image description here

In the attribute table of the Layer I have a column called "Bild" with the path of the picture.

enter image description here

In the layer properties I set the path to the image as well. When I use a automatic created form I can display the image:

enter image description here

However, when I choose my prepared .ui Form I can't display the image:

enter image description here

Is it possible to display the image I created in my .ui form? And if yes where is my mistake? I use QGIS 3.28.

1 Answer 1

1

The pixmap attribute of QLabel won't let you refer to a dynamic field value via QtDesigner, only let you load a static image from an absolute file path.

I think using QLabel itself to dynamically display images was an old method used in QGIS 2.x with the deprecated Photo QGIS widget type.


QgsExternalResourceWidget

From 3.x it appears the direct alternative is to useQgsExternalResourceWidget (ref github issue) instead, along with the Attachment QGIS widget type.

Instructions:

  • In QGIS, set up the field to use the Attachment widget:

    • Go to Layer Properties > Attribute Form, set the Bild widget type to Attachment
    • Store path as = Relative to Project Path
    • Integrated Document Viewer = Image
    • Display Resource Path = disabled, if you only want to see the image and no file path, as in your example
  • In QtDesigner, add a QgsExternalResourceWidget to your form and name it Bild (objectName property).

    • Some of the other properties (e.g. relativeStorage, fileWidgetVisible) seem to be overridden by the QGIS widget settings described above so may not be worth setting up here.

Result:

enter image description here


QTextBrowser + HTML

Alternatively, you can try a QWidget container then adding a TextBrowser widget inside it, alongside the QGIS Text Edit widget (enable Multiline and HTML) as explained in this answer for QGIS 3.6.

However, a couple of drawbacks:

  • Instead of the Bild field as-is, as the link explains, you would need to use a Virtual Field that adds the <img> tags to the field value, and in your case, also generating the full file path, so an expression like:

    '<img src="' || @project_folder || '\\' || "Bild" || '">'
    

    And name the QTextBrowser widget after the Virtual Field

  • The result will not auto resize the image by default or in any easy way, I think you need to specify it in the Virtual Field HTML or maybe fiddle around with the QWidget / QTextBrowser properties. By default it would look something like the below.

    enter image description here

1
  • Wonderful! the "QgsExternalResourceWidget" solution works perfectly for me. Thank you very much
    – Dennis
    Mar 17 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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