6

I'd like to integrate an UI file as a form in QGIS 2.8. The UI file is created in QT Designer. One of the attributes in the attribute table is a filepath to an image. I'd like to display the picture in the form instead of the filepath. I created lineEdits for each attribute and named them properly, so everything works well, except for the picture.

I load all the layers from my Qgis plugin and I set "Photo" as an EditType for that attribute in the script (You can find it also in layer Properties/Fields). So when I choose "Autogenerate" form, the picture is displayed correctly.

Could anyone give me a hint how could I make the form work correctly?

  • hi when i use this nice soulution, i constantly get Traceback (most recent call last): File "", line 1, in File "C:/aquon\startForm.py", line 13, in formOpen fotoPath=attr[5] IndexError: list index out of range When accessing the attribute table, the picture is correcty shown – Elmalle Pietje Sep 14 '15 at 16:33
  • can't figure out how to make it work... now its trying to put Qlabel into a Qwidget ... so i get: AttributeError: 'NoneType' object has no attribute 'setPixmap' – Elmalle Pietje Sep 14 '15 at 17:58
  • Instead of adding a new answer, please leave a comment for the answer which you are trying to implement with the "add a comment" link below the answer. Please give some information about what you tried. Please note that the two answers above are completely different approaches which should not be mixed (i.e. my answer does not require any python at all). – Matthias Kuhn Sep 14 '15 at 18:06
  • @ElmallePietje I think the reason might be that you didn't declare a list ' attr ' or it's empty. Try to create one with a path to a photo in the proper place of the list (attr[5]). – Krzychu Sep 21 '15 at 11:09
4

After hours of trying I found a solution. It was inspired by this post: http://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/

It's not possible to link the attribute from the table with UI file directly. To display a photo in a custom form, the user needs an addtitional python script. Let's call it startForm.py . Its content is as follows:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

fotoField = None
myDialog = None
layerField=None
objectField=None

def formOpen(dialog,layerid,featureid):
  global myDialog
  myDialog = dialog
  global nameField, layerField, objectField
  attr=featureid.attributes()
  fotoField = dialog.findChild(QLabel,"Foto")
  fotoPath=attr[5] ##the file path to the image is stored right here
  fotoField.setPixmap(QPixmap(fotoPath))
  layerField=dialog.findChild(QLabel,"label_2")
  objectField=dialog.findChild(QLabel,"label_3")
  layerField.setText(layerid.name())
  objectField.setText(str(featureid.id()))

The script displays a picture in the form, as well as it changes the layer name and the feature id. I load all my layers from a plugin window and I add those 2 lines to the code, so that each layer is already linked with the form:

##A piece of my plugin code; section: insert layers
alayer=iface.activeLayer()
editForm=self.plugin_dir+"\prForma.ui"
alayer.setEditForm(editForm)
alayer.setEditFormInit("startForm.formOpen")

In this case, the UI file and the startForm.py should be stored in the plugin directory.

The result:

enter image description here

Good luck!

  • Well done, thanks for sharing the solution! +1 – Joseph May 12 '15 at 9:54
4

The solution which does not require python is

In QtDesigner

  1. Add a new QWidget (or any other widget which serves as a container) and in the properties set the objectName to match the field's name. (I.e. If the field name is photo call it photo)
  2. Put a new QLabel or QWebView inside the container. If there are multiple QLabels inside you can specify the correct one with the objectName PhotoLabel.

In QGIS

  1. Configure the widget in the layer properties and set "Photo" as type
  • Seems there is a bug when the default size of the photo widget in qgis is 0 for the height and the width. I had to put a default size manually. Tested on windows on qgis 2.12 --> 2.18 – lejedi76 Nov 16 '16 at 15:45

protected by Community Oct 9 '18 at 21:14

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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