Skip to content
Permalink
Browse files
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 112754fa69b0ac658373bd52a13ab0312c97232c
Showing with 30 additions and 2 deletions.
  1. +2 −1 src/gui/qgsattributeform.cpp
  2. +1 −0 tests/Testing/Temporary/CTestCostData.txt
  3. +27 −1 tests/src/python/test_qgsattributeform.py
@@ -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 );
}
}
}
@@ -0,0 +1 @@
---
@@ -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()

@@ -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.