Skip to content

Commit 7028457

Browse files
committed
fix crash deleting expression field when layer is not editable
1 parent 5b0bfc6 commit 7028457

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,6 +2954,7 @@ const QList< QgsVectorJoinInfo > QgsVectorLayer::vectorJoins() const
29542954

29552955
int QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld )
29562956
{
2957+
emit beforeAddingExpressionField( fld.name() );
29572958
mExpressionFieldBuffer->addExpression( exp, fld );
29582959
updateFields();
29592960
int idx = mUpdatedFields.indexFromName( fld.name() );
@@ -2963,6 +2964,7 @@ int QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld
29632964

29642965
void QgsVectorLayer::removeExpressionField( int index )
29652966
{
2967+
emit beforeRemovingExpressionField( index );
29662968
int oi = mUpdatedFields.fieldOriginIndex( index );
29672969
mExpressionFieldBuffer->removeExpression( oi );
29682970
updateFields();

src/core/qgsvectorlayer.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,22 +1874,37 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
18741874

18751875
/**
18761876
* Will be emitted, when a new attribute has been added to this vector layer.
1877-
* Applies only to types {@link QgsFields::OriginEdit} and {@link QgsFields::OriginProvider}
1877+
* Applies only to types {@link QgsFields::OriginEdit}, {@link QgsFields::OriginProvider} and {@link QgsFields::OriginExpression }
18781878
*
18791879
* @param idx The index of the new attribute
18801880
*
18811881
* @see updatedFields()
18821882
*/
18831883
void attributeAdded( int idx );
1884+
/**
1885+
* Will be emitted, when an expression field is going to be added to this vector layer.
1886+
* Applies only to types {@link QgsFields::OriginExpression }
1887+
*
1888+
* @param fieldName The name of the attribute to be added
1889+
*/
1890+
void beforeAddingExpressionField( QString fieldName );
18841891
/**
18851892
* Will be emitted, when an attribute has been deleted from this vector layer.
1886-
* Applies only to types {@link QgsFields::OriginEdit} and {@link QgsFields::OriginProvider}
1893+
* Applies only to types {@link QgsFields::OriginEdit}, {@link QgsFields::OriginProvider} and {@link QgsFields::OriginExpression }
18871894
*
18881895
* @param idx The index of the deleted attribute
18891896
*
18901897
* @see updatedFields()
18911898
*/
18921899
void attributeDeleted( int idx );
1900+
/**
1901+
* Will be emitted, when an expression field is going to be deleted from this vector layer.
1902+
* Applies only to types {@link QgsFields::OriginExpression }
1903+
*
1904+
* @param idx The index of the attribute to be deleted
1905+
*/
1906+
void beforeRemovingExpressionField( int idx );
1907+
18931908
/**
18941909
* Emitted when a new feature has been added to the layer
18951910
*

src/gui/qgsattributeform.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ QgsAttributeForm::QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &featur
4545
, mFormNr( sFormCounter++ )
4646
, mIsSaving( false )
4747
, mIsAddDialog( false )
48+
, mPreventFeatureRefresh( false )
4849
, mEditCommandMessage( tr( "Attributes changed" ) )
4950
{
5051
init();
@@ -53,6 +54,8 @@ QgsAttributeForm::QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &featur
5354

5455
connect( vl, SIGNAL( attributeAdded( int ) ), this, SLOT( onAttributeAdded( int ) ) );
5556
connect( vl, SIGNAL( attributeDeleted( int ) ), this, SLOT( onAttributeDeleted( int ) ) );
57+
connect( vl, SIGNAL( beforeAddingExpressionField( QString ) ), this, SLOT( preventFeatureRefresh() ) );
58+
connect( vl, SIGNAL( beforeRemovingExpressionField( int ) ), this, SLOT( preventFeatureRefresh() ) );
5659
}
5760

5861
QgsAttributeForm::~QgsAttributeForm()
@@ -274,7 +277,7 @@ void QgsAttributeForm::onAttributeChanged( const QVariant& value )
274277

275278
void QgsAttributeForm::onAttributeAdded( int idx )
276279
{
277-
Q_UNUSED( idx ) // only used for Q_ASSERT
280+
mPreventFeatureRefresh = false;
278281
if ( mFeature.isValid() )
279282
{
280283
QgsAttributes attrs = mFeature.attributes();
@@ -288,6 +291,7 @@ void QgsAttributeForm::onAttributeAdded( int idx )
288291

289292
void QgsAttributeForm::onAttributeDeleted( int idx )
290293
{
294+
mPreventFeatureRefresh = false;
291295
if ( mFeature.isValid() )
292296
{
293297
QgsAttributes attrs = mFeature.attributes();
@@ -299,9 +303,14 @@ void QgsAttributeForm::onAttributeDeleted( int idx )
299303
setFeature( mFeature );
300304
}
301305

306+
void QgsAttributeForm::preventFeatureRefresh()
307+
{
308+
mPreventFeatureRefresh = true;
309+
}
310+
302311
void QgsAttributeForm::refreshFeature()
303312
{
304-
if ( mLayer->isEditable() || !mFeature.isValid() )
313+
if ( mPreventFeatureRefresh || mLayer->isEditable() || !mFeature.isValid() )
305314
return;
306315

307316
// reload feature if layer changed although not editable

src/gui/qgsattributeform.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
174174
void onAttributeAdded( int idx );
175175
void onAttributeDeleted( int idx );
176176

177+
void preventFeatureRefresh();
177178
void synchronizeEnabledState();
178179

179180
private:
@@ -210,6 +211,9 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
210211
bool mIsSaving;
211212
bool mIsAddDialog;
212213

214+
//! Flag to prevent refreshFeature() to change mFeature
215+
bool mPreventFeatureRefresh;
216+
213217
QString mEditCommandMessage;
214218
};
215219

0 commit comments

Comments
 (0)