Skip to content

Commit 089d68f

Browse files
authoredDec 11, 2021
Merge pull request #46439 from elpaso/bugfix-gh46369-value-relation-null-filtering
Treat empty array as NULL; and fix value relation filter with NULL
2 parents 3864026 + a133362 commit 089d68f

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed
 

‎src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ QSet<QString> QgsValueRelationFieldFormatter::expressionParentFormAttributes( co
331331
QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[f->fnIndex()];
332332
if ( formFunctions.contains( fd->name( ) ) )
333333
{
334-
for ( const auto &param : f->args( )->list() )
334+
const QList<QgsExpressionNode *> cExpressionNodes { f->args( )->list() };
335+
for ( const auto &param : std::as_const( cExpressionNodes ) )
335336
{
336337
attributes.insert( param->eval( &exp, &context ).toString() );
337338
}
@@ -355,7 +356,8 @@ QSet<QString> QgsValueRelationFieldFormatter::expressionFormAttributes( const QS
355356
QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[f->fnIndex()];
356357
if ( formFunctions.contains( fd->name( ) ) )
357358
{
358-
for ( const auto &param : f->args( )->list() )
359+
const QList<QgsExpressionNode *> cExpressionNodes { f->args( )->list() };
360+
for ( const auto &param : std::as_const( cExpressionNodes ) )
359361
{
360362
attributes.insert( param->eval( &exp, &context ).toString() );
361363
}
@@ -371,7 +373,7 @@ bool QgsValueRelationFieldFormatter::expressionIsUsable( const QString &expressi
371373
const QSet<QString> attrs = expressionFormAttributes( expression );
372374
for ( auto it = attrs.constBegin() ; it != attrs.constEnd(); it++ )
373375
{
374-
if ( ! feature.attribute( *it ).isValid() )
376+
if ( feature.fieldNameIndex( *it ) < 0 )
375377
return false;
376378
}
377379

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ QVariant QgsValueRelationWidgetWrapper::value() const
7777
}
7878
}
7979

80+
// If there is no selection and allow NULL is not checked return invalid.
81+
if ( selection.isEmpty() && ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
82+
{
83+
return QVariant();
84+
}
85+
8086
QVariantList vl;
8187
//store as QVariantList because the field type supports data structure
8288
for ( const QString &s : std::as_const( selection ) )

‎tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,10 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()
13371337

13381338
// FEATURE 4
13391339
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );
1340+
// Because allowNull is false we have an invalid variant here
1341+
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
1342+
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
1343+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
13401344
//check if first feature checked correctly (NULL)
13411345
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );
13421346
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
@@ -1346,9 +1350,16 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()
13461350
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
13471351
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
13481352
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
1353+
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = false;
1354+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
13491355

13501356
// FEATURE 5
13511357
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
1358+
// Because allowNull is false we have an invalid variant here
1359+
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
1360+
1361+
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
1362+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
13521363
//check if first feature checked correctly (blank)
13531364
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
13541365
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
@@ -1495,6 +1506,11 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
14951506
// FEATURE 4
14961507
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );
14971508

1509+
// Because allowNull is false we have an invalid variant here
1510+
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
1511+
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
1512+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
1513+
14981514
//check if first feature checked correctly (NULL)
14991515
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
15001516

@@ -1505,10 +1521,17 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
15051521
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
15061522
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
15071523
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
1524+
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = false;
1525+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
15081526

15091527
// FEATURE 5
15101528
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
15111529

1530+
// Because allowNull is false we have an invalid variant here
1531+
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
1532+
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
1533+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
1534+
15121535
//check if first feature checked correctly (blank)
15131536
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );
15141537

0 commit comments

Comments
 (0)
Please sign in to comment.