Skip to content
Permalink
Browse files
Valu relation widget: fix allowMulti + not null
Fix #46366 by changing the returned value from the
widget when allowMulti is ON and allowNull is OFF,
in this case when the user makes no selection
an invalid QVariant is returned instead of a (valid)
empty list of QVariant(s).
  • Loading branch information
elpaso authored and m-kuhn committed Dec 27, 2021
1 parent fb0d466 commit 51a06d38d164b7425b92aca43756acb6d46c13d0
Showing with 29 additions and 0 deletions.
  1. +6 −0 src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
  2. +23 −0 tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp
@@ -76,6 +76,12 @@ QVariant QgsValueRelationWidgetWrapper::value() const
}
}

// If there is no selection and allow NULL is not checked return invalid.
if ( selection.isEmpty() && ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
return QVariant();
}

QVariantList vl;
//store as QVariantList because the field type supports data structure
for ( const QString &s : qgis::as_const( selection ) )
@@ -1337,6 +1337,10 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()

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

// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );

cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );
//check if first feature checked correctly (blank)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
@@ -1496,6 +1507,11 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );

// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

//check if first feature checked correctly (NULL)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );

@@ -1506,10 +1522,17 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = false;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

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

// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

//check if first feature checked correctly (blank)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );

0 comments on commit 51a06d3

Please sign in to comment.