Skip to content
Permalink
Browse files

Merge pull request #6312 from nyalldawson/field_exp_null

Don't silently discard requests to set QgsFieldExpressionWidget to an empty expression
  • Loading branch information
nyalldawson committed Feb 12, 2018
2 parents ac28714 + 9dca6da commit efdaf5fc00972621e417ad0031d960cfe1f1a557
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 efdaf5f

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