@@ -44,7 +44,6 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
44
44
: QAbstractTableModel( parent )
45
45
, mLayerCache( layerCache )
46
46
, mFieldCount( 0 )
47
- , mSortCacheExpression( QLatin1String( " " ) )
48
47
, mSortFieldIndex( -1 )
49
48
, mExtraColumns( 0 )
50
49
{
@@ -212,19 +211,19 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid, bool resettingModel
212
211
213
212
if ( featOk && mFeatureRequest .acceptFeature ( mFeat ) )
214
213
{
215
- if ( mSortFieldIndex == -1 )
216
- {
217
- mExpressionContext .setFeature ( mFeat );
218
- mSortCache [mFeat .id ()] = mSortCacheExpression .evaluate ( &mExpressionContext );
219
- }
220
- else
214
+ if ( mSortFieldIndex >= 0 )
221
215
{
222
216
QgsFieldFormatter *fieldFormatter = mFieldFormatters .at ( mSortFieldIndex );
223
217
const QVariant &widgetCache = mAttributeWidgetCaches .at ( mSortFieldIndex );
224
218
const QVariantMap &widgetConfig = mWidgetConfigs .at ( mSortFieldIndex );
225
219
QVariant sortValue = fieldFormatter->representValue ( layer (), mSortFieldIndex , widgetConfig, widgetCache, mFeat .attribute ( mSortFieldIndex ) );
226
220
mSortCache .insert ( mFeat .id (), sortValue );
227
221
}
222
+ else if ( mSortCacheExpression .isValid () )
223
+ {
224
+ mExpressionContext .setFeature ( mFeat );
225
+ mSortCache [mFeat .id ()] = mSortCacheExpression .evaluate ( &mExpressionContext );
226
+ }
228
227
229
228
// Skip if the fid is already in the map (do not add twice)!
230
229
if ( ! mIdRowMap .contains ( fid ) )
@@ -799,7 +798,14 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString )
799
798
mSortCache .clear ();
800
799
mSortCacheAttributes .clear ();
801
800
mSortFieldIndex = -1 ;
802
- mSortCacheExpression = QgsExpression ( expressionString );
801
+ if ( !expressionString.isEmpty () )
802
+ mSortCacheExpression = QgsExpression ( expressionString );
803
+ else
804
+ {
805
+ // no sorting
806
+ mSortCacheExpression = QgsExpression ();
807
+ return ;
808
+ }
803
809
804
810
QgsFieldFormatter *fieldFormatter = nullptr ;
805
811
QVariant widgetCache;
@@ -852,7 +858,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString )
852
858
853
859
QString QgsAttributeTableModel::sortCacheExpression () const
854
860
{
855
- if ( mSortCacheExpression .rootNode () )
861
+ if ( mSortCacheExpression .isValid () )
856
862
return mSortCacheExpression .expression ();
857
863
else
858
864
return QString ();
0 commit comments