Skip to content

Commit 672b994

Browse files
committedNov 8, 2018
createFeature on move and copy
using of the createFeature function with tool move and copy and there the logic of unique not null fields (like primary keys) is used. This would fix #20347 (cherry-picked from 4ac0631)
1 parent bf10953 commit 672b994

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed
 

‎src/core/qgsvectorlayertools.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsvectorlayertools.h"
1919
#include "qgsfeaturerequest.h"
2020
#include "qgslogger.h"
21+
#include "qgsvectorlayerutils.h"
2122

2223

2324
QgsVectorLayerTools::QgsVectorLayerTools()
@@ -34,7 +35,6 @@ bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureReq
3435

3536
QgsFeatureIterator fi = layer->getFeatures( request );
3637
QgsFeature f;
37-
QgsAttributeList pkAttrList = layer->primaryKeyAttributes();
3838

3939
int browsedFeatureCount = 0;
4040
int couldNotWriteCount = 0;
@@ -45,29 +45,27 @@ bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureReq
4545
while ( fi.nextFeature( f ) )
4646
{
4747
browsedFeatureCount++;
48-
// remove pkey values
49-
Q_FOREACH ( auto idx, pkAttrList )
50-
{
51-
f.setAttribute( idx, QVariant() );
52-
}
48+
49+
QgsFeature newFeature = QgsVectorLayerUtils::createFeature( layer, f.geometry(), f.attributes().toMap() );
50+
5351
// translate
54-
if ( f.hasGeometry() )
52+
if ( newFeature.hasGeometry() )
5553
{
56-
QgsGeometry geom = f.geometry();
54+
QgsGeometry geom = newFeature.geometry();
5755
geom.translate( dx, dy );
58-
f.setGeometry( geom );
56+
newFeature.setGeometry( geom );
5957
#ifdef QGISDEBUG
60-
const QgsFeatureId fid = f.id();
58+
const QgsFeatureId fid = newFeature.id();
6159
#endif
6260
// paste feature
63-
if ( !layer->addFeature( f ) )
61+
if ( !layer->addFeature( newFeature ) )
6462
{
6563
couldNotWriteCount++;
6664
QgsDebugMsg( QStringLiteral( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) );
6765
}
6866
else
6967
{
70-
fidList.insert( f.id() );
68+
fidList.insert( newFeature.id() );
7169
}
7270
}
7371
else

0 commit comments

Comments
 (0)
Please sign in to comment.