Skip to content

Commit

Permalink
update display expression when adding fields through attribute table (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jan 27, 2022
1 parent 80f2443 commit 46307f7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,14 +890,18 @@ void QgsAttributeTableDialog::mActionAddAttribute_triggered()
if ( mLayer->addAttribute( dialog.field() ) )
{
mLayer->endEditCommand();

if ( mLayer->displayExpression().isEmpty() )
{
mLayer->setDisplayExpression( dialog.field().name() );
}
}
else
{
mLayer->destroyEditCommand();
QMessageBox::critical( this, tr( "Add Field" ), tr( "Failed to add field '%1' of type '%2'. Is the field name unique?" ).arg( dialog.field().name(), dialog.field().typeName() ) );
}


// update model - a field has been added or updated
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
}
Expand All @@ -919,12 +923,17 @@ void QgsAttributeTableDialog::mActionRemoveAttribute_triggered()
return;
}

// check whether display expression is a single field
int fieldIdx = QgsExpression::expressionToLayerFieldIndex( mLayer->displayExpression(), mLayer );
QgsAttributeTableModel *masterModel = mMainView->masterModel();

mLayer->beginEditCommand( tr( "Deleted attribute" ) );
if ( mLayer->deleteAttributes( attributes ) )
{
mLayer->endEditCommand();

if ( fieldIdx != -1 && attributes.contains( fieldIdx ) )
mLayer->setDisplayExpression( mLayer->fields().count() > 0 ? mLayer->fields().at( 0 ).name() : QString() );
}
else
{
Expand Down

0 comments on commit 46307f7

Please sign in to comment.