Skip to content

Commit

Permalink
fix edge case where fkey is 0 in relation reference widget
Browse files Browse the repository at this point in the history
the list of identifiers was initialized with a list QVariant(Int) for Null, but they are equal to 0
now, we also control that the value is valid to determine if the identifier is not null
  • Loading branch information
3nids committed Sep 23, 2020
1 parent 194a517 commit 756b293
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/core/qgsfeaturefiltermodel.cpp
Expand Up @@ -96,7 +96,7 @@ bool QgsFeatureFilterModel::identifierIsNull( const QVariant &identifier ) const
const QVariantList values = identifier.toList();
for ( const QVariant &value : values )
{
if ( !value.isNull() )
if ( !value.isNull() && value.isValid() )
{
return false;
}
Expand Down Expand Up @@ -151,6 +151,6 @@ void QgsFeatureFilterModel::setExtraIdentifierValues( const QVariantList &extraI

void QgsFeatureFilterModel::setExtraIdentifierValueToNull()
{
setExtraIdentifierValue( QVariantList() );
setExtraIdentifierValue( nullIdentifier() );
}

2 changes: 1 addition & 1 deletion src/core/qgsfeaturepickermodelbase.cpp
Expand Up @@ -612,7 +612,7 @@ void QgsFeaturePickerModelBase::reload()

void QgsFeaturePickerModelBase::setExtraIdentifierValue( const QVariant &extraIdentifierValue )
{
if ( extraIdentifierValue == mExtraIdentifierValue && !identifierIsNull( extraIdentifierValue ) )
if ( extraIdentifierValue == mExtraIdentifierValue && !identifierIsNull( extraIdentifierValue ) && !identifierIsNull( mExtraIdentifierValue ) )
return;

if ( mIsSettingExtraIdentifierValue )
Expand Down
6 changes: 3 additions & 3 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -54,7 +54,7 @@ bool qVariantListIsNull( const QVariantList &list )

for ( int i = 0; i < list.size(); ++i )
{
if ( !list.at( i ).isNull() )
if ( !list.at( i ).isNull() && list.at( i ).isValid() )
return false;
}
return true;
Expand Down Expand Up @@ -599,7 +599,7 @@ void QgsRelationReferenceWidget::init()
}

// Only connect after iterating, to have only one iterator on the referenced table at once
connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRelationReferenceWidget::comboReferenceChanged );
connect( mComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &QgsRelationReferenceWidget::comboReferenceChanged );

QApplication::restoreOverrideCursor();

Expand Down Expand Up @@ -1074,7 +1074,7 @@ void QgsRelationReferenceWidget::disableChainedComboBoxes( const QComboBox *scb

void QgsRelationReferenceWidget::emitForeignKeysChanged( const QVariantList &foreignKeys, bool force )
{
if ( foreignKeys == mForeignKeys && force == false )
if ( foreignKeys == mForeignKeys && force == false && qVariantListIsNull( foreignKeys ) == qVariantListIsNull( mForeignKeys ) )
return;

mForeignKeys = foreignKeys;
Expand Down
13 changes: 9 additions & 4 deletions tests/src/gui/testqgsrelationreferencewidget.cpp
Expand Up @@ -518,22 +518,27 @@ void TestQgsRelationReferenceWidget::testSetGetForeignKey()
w.setRelation( *mRelation, true );
w.init();

QSignalSpy spy( &w, SIGNAL( foreignKeyChanged( QVariant ) ) );
QSignalSpy spy( &w, &QgsRelationReferenceWidget::foreignKeysChanged );

w.setForeignKeys( QVariantList() << 0 );
QCOMPARE( w.foreignKeys().at( 0 ), QVariant( 0 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(0)" ) );
QCOMPARE( spy.count(), 1 );

w.setForeignKeys( QVariantList() << 11 );
QCOMPARE( w.foreignKeys().at( 0 ), QVariant( 11 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(11)" ) );
QCOMPARE( spy.count(), 1 );
QCOMPARE( spy.count(), 2 );

w.setForeignKeys( QVariantList() << 12 );
QCOMPARE( w.foreignKeys().at( 0 ), QVariant( 12 ) );
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "(12)" ) );
QCOMPARE( spy.count(), 2 );
QCOMPARE( spy.count(), 3 );

w.setForeignKeys( QVariantList() << QVariant() );
QVERIFY( w.foreignKeys().at( 0 ).isNull() );
QVERIFY( w.foreignKeys().at( 0 ).isValid() );
QCOMPARE( spy.count(), 3 );
QCOMPARE( spy.count(), 4 );
}

// Test issue https://github.com/qgis/QGIS/issues/29884
Expand Down

0 comments on commit 756b293

Please sign in to comment.