2

I have a layer with a virtual field, "LongLabel", an expression concatenating several of its data fields. When I edit a feature's data fields in the layer's attribute table/form, how can I force the virtual field to update for that feature? I care since "LongLabel" is the feature selector field on the left hand side of the form.

I'm unsurprised "LongLabel" doesn't update immediately, while I remain in that feature in the table. But I was hoping it would update after I switch to a different feature and come back, or when I save my edits and/or reload the table (icons at top), or - at worst - if I toggle editing on and off to force a data provider commit. No change.

The edits to the underlying data fields are correctly captured, it's just the virtual field that doesn't recalc. After the data edit, if I open a 2nd attribute table (F6 from canvas) it correctly displays the updated "LongLabel" in the 2nd table but the unupdated one in the original table.

If there's no way to do it from the QGIS UI, I'd be fine with a PyQGIS solution. An iface.activeLayer().dataProvider().forceReload() from the Python console doesn't help though. An .activeLayer().commitChanges() followed by .startEditing() toggles editing on and off, reflected in the attr table's editing icon, but still not updating the field. I can also find the attribute table widget by searching for 'QgsAttributeTableDialog/'+iface.activeLayer().id() in [x.objectName() for x in QApplication.instance().allWidgets()] but can't figure out how to force it to do anything other than .close() once I've located it.

Editing to add: I've gone and replaced "LongLabel" with its expression definition in the layer's display name property. In this case, the LHS feature selector (that used to be "LongLabel") correctly updates when I move away from the edited feature. However, "LongLabel" as a field in the attribute form still does not update. So it's related specifically to virtual fields. Using QGis 3.10.0.

  • stop edit + reload should refresh the field (in 3.4 at least) – JGH Nov 1 '19 at 13:08
  • @JGH, thanks, would be sensible, but (at least for me in 3.10 it doesn't. Underlying layer is a gpkg. – Houska Nov 1 '19 at 13:14
2

I have yet to get Virtual Field data to update in the Attribute Table.

However, instead of using virtual fields, could you consider using 'default expressions' in Attributes? These now auto-update in the attribute table from 3.10 onwards (see changelog) (It does mean you need to add an actual field though, so if you do not have write access this will not be an option - but seeing as you are editing data anyway...)

To set this up, add "LongLabel" as an actual field, then to go to Layer Properties > Attributes Form > select LongLabel under Available Widgets, and enter the relevant expression under Defaults, checking Apply default value on update.

enter image description here

Now whenever you change the dependent fields, the dynamic field will auto-update whether you're in the attribute table or editing the feature form. Very cool!

  • Thank you. Will explore; indeed the "real-time" updating in the changelog for 3.10 is helpful. I already use an auto-updating DateUpdated field in the layer form. I do unfortunately access the same layer data in 2 projects with slightly different definitions of LongLabel, but there are workarounds for that if need be. – Houska Nov 9 '19 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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