@@ -6154,15 +6154,33 @@ void QgisApp::mergeAttributesOfSelectedFeatures()
6154
6154
QgsAttributes merged = d.mergedAttributes ();
6155
6155
QSet<int > toSkip = d.skippedAttributeIndexes ();
6156
6156
6157
+ bool firstFeature = true ;
6157
6158
Q_FOREACH ( QgsFeatureId fid, vl->selectedFeaturesIds () )
6158
6159
{
6159
6160
for ( int i = 0 ; i < merged.count (); ++i )
6160
6161
{
6161
6162
if ( toSkip.contains ( i ) )
6162
6163
continue ;
6163
6164
6164
- vl->changeAttributeValue ( fid, i, merged.at ( i ) );
6165
+ QVariant val = merged.at ( i );
6166
+ // convert to destination data type
6167
+ if ( ! vl->fields ()[i].convertCompatible ( val ) )
6168
+ {
6169
+ if ( firstFeature )
6170
+ {
6171
+ // only warn on first feature
6172
+ messageBar ()->pushMessage (
6173
+ tr ( " Invalid result" ),
6174
+ tr ( " Could not store value '%1' in field of type %2" ).arg ( merged.at ( i ).toString (), vl->fields ()[i].typeName () ),
6175
+ QgsMessageBar::WARNING );
6176
+ }
6177
+ }
6178
+ else
6179
+ {
6180
+ vl->changeAttributeValue ( fid, i, val );
6181
+ }
6165
6182
}
6183
+ firstFeature = false ;
6166
6184
}
6167
6185
6168
6186
vl->endEditCommand ();
@@ -6277,7 +6295,22 @@ void QgisApp::mergeSelectedFeatures()
6277
6295
// create new feature
6278
6296
QgsFeature newFeature;
6279
6297
newFeature.setGeometry ( unionGeom );
6280
- newFeature.setAttributes ( d.mergedAttributes () );
6298
+
6299
+ QgsAttributes attrs = d.mergedAttributes ();
6300
+ for ( int i = 0 ; i < attrs.count (); ++i )
6301
+ {
6302
+ QVariant val = attrs.at ( i );
6303
+ // convert to destination data type
6304
+ if ( ! vl->fields ()[i].convertCompatible ( val ) )
6305
+ {
6306
+ messageBar ()->pushMessage (
6307
+ tr ( " Invalid result" ),
6308
+ tr ( " Could not store value '%1' in field of type %2" ).arg ( attrs.at ( i ).toString (), vl->fields ()[i].typeName () ),
6309
+ QgsMessageBar::WARNING );
6310
+ }
6311
+ attrs[i] = val;
6312
+ }
6313
+ newFeature.setAttributes ( attrs );
6281
6314
6282
6315
QgsFeatureIds::const_iterator feature_it = featureIdsAfter.constBegin ();
6283
6316
for ( ; feature_it != featureIdsAfter.constEnd (); ++feature_it )
0 commit comments