Skip to content

Commit c576281

Browse files
committed
Fix merge attributes/features tool resets values to null for int fields
Also add a warning if merged attribute value is not compatible with field type. Fix #12842 (cherry-picked from 099a40b)
1 parent 45d80f9 commit c576281

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/app/qgisapp.cpp

+35-2
Original file line numberDiff line numberDiff line change
@@ -5891,15 +5891,33 @@ void QgisApp::mergeAttributesOfSelectedFeatures()
58915891
QgsAttributes merged = d.mergedAttributes();
58925892
QSet<int> toSkip = d.skippedAttributeIndexes();
58935893

5894+
bool firstFeature = true;
58945895
Q_FOREACH ( QgsFeatureId fid, vl->selectedFeaturesIds() )
58955896
{
58965897
for ( int i = 0; i < merged.count(); ++i )
58975898
{
58985899
if ( toSkip.contains( i ) )
58995900
continue;
59005901

5901-
vl->changeAttributeValue( fid, i, merged.at( i ) );
5902+
QVariant val = merged.at( i );
5903+
// convert to destination data type
5904+
if ( ! vl->pendingFields()[i].convertCompatible( val ) )
5905+
{
5906+
if ( firstFeature )
5907+
{
5908+
//only warn on first feature
5909+
messageBar()->pushMessage(
5910+
tr( "Invalid result" ),
5911+
tr( "Could not store value '%1' in field of type %2" ).arg( merged.at( i ).toString(), vl->pendingFields()[i].typeName() ),
5912+
QgsMessageBar::WARNING );
5913+
}
5914+
}
5915+
else
5916+
{
5917+
vl->changeAttributeValue( fid, i, val );
5918+
}
59025919
}
5920+
firstFeature = false;
59035921
}
59045922

59055923
vl->endEditCommand();
@@ -6014,7 +6032,22 @@ void QgisApp::mergeSelectedFeatures()
60146032
//create new feature
60156033
QgsFeature newFeature;
60166034
newFeature.setGeometry( unionGeom );
6017-
newFeature.setAttributes( d.mergedAttributes() );
6035+
6036+
QgsAttributes attrs = d.mergedAttributes();
6037+
for ( int i = 0; i < attrs.count(); ++i )
6038+
{
6039+
QVariant val = attrs.at( i );
6040+
// convert to destination data type
6041+
if ( ! vl->pendingFields()[i].convertCompatible( val ) )
6042+
{
6043+
messageBar()->pushMessage(
6044+
tr( "Invalid result" ),
6045+
tr( "Could not store value '%1' in field of type %2" ).arg( attrs.at( i ).toString(), vl->pendingFields()[i].typeName() ),
6046+
QgsMessageBar::WARNING );
6047+
}
6048+
attrs[i] = val;
6049+
}
6050+
newFeature.setAttributes( attrs );
60186051

60196052
QgsFeatureIds::const_iterator feature_it = featureIdsAfter.constBegin();
60206053
for ( ; feature_it != featureIdsAfter.constEnd(); ++feature_it )

0 commit comments

Comments
 (0)