Skip to content
Permalink
Browse files

Fix crashes in expression widget when no layer is available

Fixes #40763
  • Loading branch information
nyalldawson committed Jan 1, 2021
1 parent ee6d797 commit e0e21a7349189fb9de89b1392932b3ba28536168
@@ -71,7 +71,7 @@ class QgsFeatureExpressionValuesGatherer: public QThread
Entry( const QgsFeatureId &_featureId, const QString &_value, const QgsVectorLayer *layer )
: featureId( _featureId )
, value( _value )
, feature( QgsFeature( layer->fields() ) )
, feature( QgsFeature( layer ? layer->fields() : QgsFields() ) )
{}

QVariantList identifierFields;
@@ -83,7 +83,7 @@ QgsFeatureExpressionValuesGatherer::Entry QgsFeatureFilterModel::createEntry( co
for ( const QVariant &v : constValues )
values << QStringLiteral( "(%1)" ).arg( v.toString() );

return QgsFeatureExpressionValuesGatherer::Entry( constValues, values.join( QLatin1Char( ' ' ) ), QgsFeature( sourceLayer()->fields() ) );
return QgsFeatureExpressionValuesGatherer::Entry( constValues, values.join( QLatin1Char( ' ' ) ), QgsFeature( sourceLayer() ? sourceLayer()->fields() : QgsFields() ) );
}

bool QgsFeatureFilterModel::compareEntries( const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b ) const
@@ -50,11 +50,14 @@ void QgsFeaturePickerModelBase::setSourceLayer( QgsVectorLayer *sourceLayer )
return;

mSourceLayer = sourceLayer;
mExpressionContext = sourceLayer->createExpressionContext();
if ( mSourceLayer )
mExpressionContext = mSourceLayer->createExpressionContext();

reload();
emit sourceLayerChanged();

setDisplayExpression( sourceLayer->displayExpression() );
if ( mSourceLayer )
setDisplayExpression( mSourceLayer->displayExpression() );
}


@@ -279,7 +282,7 @@ void QgsFeaturePickerModelBase::updateCompleter()
// We got strings for a filter selection
std::sort( entries.begin(), entries.end(), []( const QgsFeatureExpressionValuesGatherer::Entry & a, const QgsFeatureExpressionValuesGatherer::Entry & b ) { return a.value.localeAwareCompare( b.value ) < 0; } );

if ( mAllowNull )
if ( mAllowNull && mSourceLayer )
{
entries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
}

0 comments on commit e0e21a7

Please sign in to comment.
You can’t perform that action at this time.