Showing with 22 additions and 8 deletions.
  1. +10 −4 src/app/qgisapp.cpp
  2. +12 −4 src/app/qgsmergeattributesdialog.cpp
14 changes: 10 additions & 4 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5742,11 +5742,9 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
for ( int idx = 0; idx < fields.count(); ++idx )
{
int dst = pasteVectorLayer->fieldNameIndex( fields[idx].name() );
if ( dst < 0 || pkAttrList.contains( dst ) )
{
// skip primary key attributes
if ( dst < 0 )
continue;
}

remap.insert( idx, dst );
}

Expand All @@ -5763,6 +5761,14 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
if ( dst < 0 )
continue;

// use default value for primary key fields if it's NOT NULL
if ( pkAttrList.contains( dst ) )
{
dstAttr[ dst ] = pasteVectorLayer->dataProvider()->defaultValue( dst );
if( !dstAttr[ dst ].isNull() )
continue;
}

dstAttr[ dst ] = srcAttr[ src ];
}

Expand Down
16 changes: 12 additions & 4 deletions src/app/qgsmergeattributesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgsmapcanvas.h"
#include "qgsrubberband.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsattributeeditor.h"

#include <limits>
Expand Down Expand Up @@ -588,18 +589,25 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
{
int idx = mTableWidget->horizontalHeaderItem( i )->data( Qt::UserRole ).toInt();

QComboBox* comboBox = qobject_cast<QComboBox *>( mTableWidget->cellWidget( 0, i ) );
if ( comboBox && comboBox->currentText() == tr( "Skip attribute" ) )
QComboBox *comboBox = qobject_cast<QComboBox *>( mTableWidget->cellWidget( 0, i ) );
if ( !comboBox )
continue;

QTableWidgetItem* currentItem = mTableWidget->item( mFeatureList.size() + 1, i );
QTableWidgetItem *currentItem = mTableWidget->item( mFeatureList.size() + 1, i );
if ( !currentItem )
continue;

if ( idx >= results.count() )
results.resize( idx + 1 ); // make sure the results vector is long enough (maybe not necessary)

results[idx] = currentItem->data( Qt::DisplayRole );
if ( comboBox->currentText() != tr( "Skip attribute" ) )
{
results[idx] = currentItem->data( Qt::DisplayRole );
}
else if ( mVectorLayer->dataProvider() )
{
results[idx] = mVectorLayer->dataProvider()->defaultValue( idx );
}
}

return results;
Expand Down