Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #46172 from m-kuhn/preserve_attribute_type_in_dial…
…og_update

Preserve attribute type in dialog update
  • Loading branch information
m-kuhn committed Nov 24, 2021
2 parents bb17d1c + c1b8722 commit 112754f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/gui/qgsattributeform.cpp
Expand Up @@ -551,8 +551,9 @@ void QgsAttributeForm::updateValuesDependenciesDefaultValues( const int originId
continue;

QgsExpressionContext context = createExpressionContext( updatedFeature );
QString value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature, &context ).toString();
const QVariant value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature, &context );
eww->setValue( value );
mCurrentFormFeature.setAttribute( eww->field().name(), value );
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Testing/Temporary/CTestCostData.txt
@@ -0,0 +1 @@
---
28 changes: 27 additions & 1 deletion tests/src/python/test_qgsattributeform.py
Expand Up @@ -22,13 +22,17 @@
QgsEditorWidgetSetup,
QgsEditFormConfig,
QgsAttributeEditorElement,
QgsDefaultValue,
QgsField
)
from qgis.gui import (
QgsAttributeForm,
QgsGui,
QgsEditorWidgetWrapper,
QgsMapCanvas
QgsMapCanvas,
QgsAttributeEditorContext
)
from qgis.PyQt.QtCore import QVariant

QGISAPP = start_app()

Expand Down Expand Up @@ -115,6 +119,28 @@ def test_duplicated_widgets(self):
for field_type, value in field_types.items():
self.checkForm(field_type, value)

def test_on_update(self):
"""Test live update"""

layer = QgsVectorLayer("Point?field=age:int", "vl", "memory")
# set default value for numbers to [1, {age}], it will depend on the field age and should update
field = QgsField('numbers', QVariant.List, 'array')
field.setEditorWidgetSetup(QgsEditorWidgetSetup('List', {}))
layer.dataProvider().addAttributes([field])
layer.updateFields()
layer.setDefaultValueDefinition(1, QgsDefaultValue('array(1, age)', True))
layer.setEditorWidgetSetup(1, QgsEditorWidgetSetup('List', {}))
layer.startEditing()
form = QgsAttributeForm(layer)
feature = QgsFeature(layer.fields())
form.setFeature(feature)
form.setMode(QgsAttributeEditorContext.AddFeatureMode)
form.changeAttribute('numbers', [12])
form.changeAttribute('age', 1)
self.assertEqual(form.currentFormFeature()['numbers'], [1, 1])
form.changeAttribute('age', 7)
self.assertEqual(form.currentFormFeature()['numbers'], [1, 7])


if __name__ == '__main__':
unittest.main()

0 comments on commit 112754f

Please sign in to comment.