I have a custom form that calculates some area values, I'd like to automatically fill the base field using the geometry of the feature if the user hasn't set some value specifically. So I have the following code:
def formOpen(dialog,layerid,featureid): global sqMField sqMField = dialog.findChild(QLineEdit,"property_area_m2") global sqFtField sqFtField = dialog.findChild(QLineEdit,"property_area_ft2") global haField haField = dialog.findChild(QLineEdit,"area_area_ha") global acreField acreField = dialog.findChild(QLineEdit,"property_area_acres") sqMField.textChanged.connect( sqMChanged ) #if sqMField.text().strip() is None: layer = QgsMapLayerRegistry.instance().mapLayers()[layerid] feature = layer.getFeature(featureid) if feature.hasGeometry(): area = feature.geometry().area() sqMField.setText(str(area))
This is called when I use the identify tool on a layer, having set this function as the
Python init function on the fields tab of the layer properties. Thus I'm assuming the QGIS fills the parameters in for me?
But I get a
KeyError error and on further checking it seems like I get a different
layerid parameter each time the dialog opens, e.g:
An error occurred during execution of following code: formOpen( _qgis_featureform_50_31, _qgis_layer_register20150529183519760_31, _qgis_feature_20170214103800202) An error occurred during execution of following code: formOpen( _qgis_featureform_51_32, _qgis_layer_register20150529183519760_32, _qgis_feature_20170214103829234)
So I could probably fix it by stripping some stuff off the
layerid but that seems wrong. Can anyone show me what I'm missing?