Skip to content

Commit c0659b9

Browse files
committed
[dualview] Fix crash when removing features
1 parent f28462a commit c0659b9

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/gui/attributetable/qgsdualview.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsexpressionbuilderdialog.h"
2424
#include "qgsattributeaction.h"
2525
#include "qgsvectordataprovider.h"
26+
#include "qgsmessagelog.h"
2627

2728
#include <QDialog>
2829
#include <QMenu>
@@ -229,6 +230,7 @@ void QgsDualView::initLayerCache( QgsVectorLayer* layer )
229230
connect( layer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
230231
connect( layer, SIGNAL( attributeAdded( int ) ), this, SLOT( attributeAdded( int ) ) );
231232
connect( layer, SIGNAL( attributeDeleted( int ) ), this, SLOT( attributeDeleted( int ) ) );
233+
connect( layer, SIGNAL( featureDeleted( QgsFeatureId ) ), this, SLOT( featureDeleted( QgsFeatureId ) ) );
232234
}
233235

234236
void QgsDualView::initModels( QgsMapCanvas* mapCanvas )
@@ -255,7 +257,7 @@ void QgsDualView::on_mFeatureList_currentEditSelectionChanged( const QgsFeature
255257
// Backup old dialog and delete only after creating the new dialog, so we can "hot-swap" the contained QgsFeature
256258
QgsAttributeDialog* oldDialog = mAttributeDialog;
257259

258-
if ( mAttributeDialog->dialog() )
260+
if ( mAttributeDialog && mAttributeDialog->dialog() )
259261
{
260262
saveEditChanges();
261263
mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );
@@ -290,6 +292,12 @@ void QgsDualView::saveEditChanges()
290292
// Get the edited feature
291293
const QgsAttributes &dst = mAttributeDialog->feature()->attributes();
292294

295+
if ( src.count() != dst.count() )
296+
{
297+
// bail out
298+
return;
299+
}
300+
293301
mLayerCache->layer()->beginEditCommand( tr( "Attributes changed" ) );
294302

295303
for ( int i = 0; i < dst.count(); ++i )
@@ -348,7 +356,7 @@ void QgsDualView::previewColumnChanged( QObject* action )
348356
void QgsDualView::editingToggled()
349357
{
350358
// Reload the attribute dialog widget and commit changes if any
351-
if ( mAttributeDialog->dialog() && mAttributeDialog->feature() )
359+
if ( mAttributeDialog && mAttributeDialog->dialog() && mAttributeDialog->feature() )
352360
{
353361
on_mFeatureList_currentEditSelectionChanged( *mAttributeDialog->feature() );
354362
}
@@ -457,6 +465,22 @@ void QgsDualView::attributeAdded( int attribute )
457465
}
458466
}
459467

468+
void QgsDualView::featureDeleted( QgsFeatureId fid )
469+
{
470+
if ( mAttributeDialog && mAttributeDialog->dialog() )
471+
{
472+
QgsFeature* feat = mAttributeDialog->feature();
473+
if ( feat )
474+
{
475+
if ( fid == feat->id() )
476+
{
477+
delete( mAttributeDialog );
478+
mAttributeDialog = NULL;
479+
}
480+
}
481+
}
482+
}
483+
460484
void QgsDualView::reloadAttribute( const int& idx )
461485
{
462486
if ( mAttributeDialog && mAttributeDialog->dialog() )

src/gui/attributetable/qgsdualview.h

+8
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
206206
*/
207207
void attributeAdded( int attribute );
208208

209+
/**
210+
* Gets called when a feature is deleted.
211+
* So it can be removed from the feature form if required.
212+
*
213+
* @param fid The feature being deleted
214+
*/
215+
void featureDeleted( QgsFeatureId fid );
216+
209217
/**
210218
* Will be called periodically, when loading layers from slow data providers.
211219
*

0 commit comments

Comments
 (0)