Skip to content

Commit

Permalink
If depending field is virtual make sure to take the up to date value
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Oct 28, 2021
1 parent a7127f8 commit 59fdd30
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -566,6 +566,9 @@ void QgsAttributeForm::updateValuesDependenciesVirtualFields( const int originId
if ( !mFeature.isValid() )
return;

// create updated Feature
QgsFeature updatedFeature = QgsFeature( mFeature );

QgsAttributes featureAttributes = mFeature.attributes();
for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{
Expand Down Expand Up @@ -595,6 +598,19 @@ void QgsAttributeForm::updateValuesDependenciesVirtualFields( const int originId
featureAttributes[fieldIndexes[i]] = srcVars[i];
}
}
updatedFeature.setAttributes( featureAttributes );

// If originIdx is a virtual field make sure it is up to date
{
QString expressionField = mLayer->expressionField( originIdx );
if ( ! expressionField.isEmpty() )
{
QgsExpressionContext context = createExpressionContext( updatedFeature );
QgsExpression exp( expressionField );
QVariant value = exp.evaluate( &context );
updatedFeature.setAttribute( originIdx, value );
}
}

// go through depending fields and update the virtual field with its expression
QList<QgsWidgetWrapper *> relevantWidgets = mVirtualFieldsDependencies.values( originIdx );
Expand All @@ -608,14 +624,11 @@ void QgsAttributeForm::updateValuesDependenciesVirtualFields( const int originId
if ( mAlreadyUpdatedFields.contains( eww->fieldIdx() ) )
continue;

// create updated Feature
QgsFeature updatedFeature = QgsFeature( mFeature );
updatedFeature.setAttributes( featureAttributes );
// Update value
QgsExpressionContext context = createExpressionContext( updatedFeature );

QgsExpression exp( mLayer->expressionField( eww->fieldIdx() ) );
QVariant value = exp.evaluate( &context );

updatedFeature.setAttribute( eww->fieldIdx(), value );
eww->setValue( value );
}
}
Expand Down

0 comments on commit 59fdd30

Please sign in to comment.