Skip to content

Commit 6215f77

Browse files
committed
Fix crash when deleting a field in attribute table (fixes #12930)
Also fixes: - multiple reloads of table (due to additional connections to cache invalidation) - crash in attribute form (on redo of deleted field when it was not the last) - crash in editor widget wrapper (during table reload)
1 parent 5e4841d commit 6215f77

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/gui/attributetable/qgsattributetablemodel.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ void QgsAttributeTableModel::loadAttributes()
336336
void QgsAttributeTableModel::loadLayer()
337337
{
338338
QgsDebugMsg( "entered." );
339+
340+
// make sure attributes are properly updated before caching the data
341+
// (emit of progress() signal may enter event loop and thus attribute
342+
// table view may be updated with inconsistent model which may assume
343+
// wrong number of attributes)
344+
loadAttributes();
345+
339346
beginResetModel();
340347

341348
if ( rowCount() != 0 )
@@ -370,9 +377,8 @@ void QgsAttributeTableModel::loadLayer()
370377

371378
emit finished();
372379

373-
connect( mLayerCache, SIGNAL( invalidated() ), this, SLOT( loadLayer() ) );
380+
connect( mLayerCache, SIGNAL( invalidated() ), this, SLOT( loadLayer() ), Qt::UniqueConnection );
374381

375-
mFieldCount = mAttributes.size();
376382
endResetModel();
377383
}
378384

src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ int QgsEditorWidgetWrapper::fieldIdx()
3333

3434
QgsField QgsEditorWidgetWrapper::field()
3535
{
36-
return layer()->pendingFields()[mFieldIdx];
36+
if ( mFieldIdx < layer()->pendingFields().count() )
37+
return layer()->pendingFields()[mFieldIdx];
38+
else
39+
return QgsField();
3740
}
3841

3942
QVariant QgsEditorWidgetWrapper::defaultValue()

src/gui/qgsattributeform.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ void QgsAttributeForm::onAttributeAdded( int idx )
277277
if ( mFeature.isValid() )
278278
{
279279
QgsAttributes attrs = mFeature.attributes();
280-
Q_ASSERT( attrs.size() == idx );
281-
attrs.append( QVariant( layer()->pendingFields()[idx].type() ) );
280+
attrs.insert( idx, QVariant( layer()->pendingFields()[idx].type() ) );
282281
mFeature.setFields( layer()->pendingFields() );
283282
mFeature.setAttributes( attrs );
284283
}

0 commit comments

Comments
 (0)