5

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.

3
  • stop edit + reload should refresh the field (in 3.4 at least)
    – JGH
    Commented Nov 1, 2019 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
    Commented Nov 1, 2019 at 13:14
  • Upvote for the clear & helpful explanation -- I thought my virtual fields weren't updating at all, and your question prompted me to open the attribute table again, where I can see the updates.
    – pete
    Commented Dec 7, 2021 at 15:12

1 Answer 1

6

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!

1
  • 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
    Commented Nov 9, 2019 at 9:45

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.