Skip to content
Permalink
Browse files

Don't silently discard requests to set QgsFieldExpressionWidget

to an empty expression

This causes issues in lots of places - e.g. a field constraint
is always set to the first field in a layer, and cannot be
removed.

I've looked at all calls to setField and without exception
they should accept that setting the field to an empty string
results in an empty expression.

Fixes #14325
  • Loading branch information
nyalldawson committed Feb 12, 2018
1 parent 46d7a42 commit 9dca6da060f17f522d5e2b33d28c74f3f6618ec2
Showing with 25 additions and 0 deletions.
  1. +3 −0 src/gui/qgsfieldexpressionwidget.cpp
  2. +22 −0 tests/src/gui/testqgsfieldexpressionwidget.cpp
@@ -173,7 +173,10 @@ void QgsFieldExpressionWidget::setLayer( QgsMapLayer *layer )
void QgsFieldExpressionWidget::setField( const QString &fieldName )
{
if ( fieldName.isEmpty() )
{
setRow( -1 );
return;
}

QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
if ( !idx.isValid() )
@@ -47,6 +47,7 @@ class TestQgsFieldExpressionWidget : public QObject
void asExpression();
void testIsValid();
void testFilters();
void setNull();

private:
QgsFieldExpressionWidget *mWidget = nullptr;
@@ -269,6 +270,27 @@ void TestQgsFieldExpressionWidget::testFilters()
QgsProject::instance()->removeMapLayer( layer );
}

void TestQgsFieldExpressionWidget::setNull()
{
// test that QgsFieldExpressionWidget can be set to an empty value
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "point?field=fld:int&field=fld2:int&field=fld3:int" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( layer );

std::unique_ptr< QgsFieldExpressionWidget > widget( new QgsFieldExpressionWidget() );
widget->setLayer( layer );

widget->setField( QString() );
QVERIFY( widget->currentField().isEmpty() );

widget->setField( QStringLiteral( "fld2" ) );
QCOMPARE( widget->currentField(), QStringLiteral( "fld2" ) );

widget->setField( QString() );
QVERIFY( widget->currentField().isEmpty() );

QgsProject::instance()->removeMapLayer( layer );
}

QGSTEST_MAIN( TestQgsFieldExpressionWidget )
#include "testqgsfieldexpressionwidget.moc"

0 comments on commit 9dca6da

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