Skip to content

Commit c76a32a

Browse files
authoredSep 11, 2020
Parentheses are needed in case the filter expression contains "OR" (#38672)
1 parent c6e2882 commit c76a32a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
 

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,10 @@ void QgsRelationReferenceWidget::filterChanged()
865865
QgsFeatureIds featureIds;
866866
QString filterExpression = mFilterExpression;
867867

868+
// wrap the expression with parentheses as it might contain `OR`
869+
if ( !filterExpression.isEmpty() )
870+
filterExpression = QStringLiteral( " ( %1 ) " ).arg( filterExpression );
871+
868872
// comboboxes have to be disabled before building filters
869873
if ( mChainFilters )
870874
disableChainedComboBoxes( scb );

‎tests/src/gui/testqgsrelationreferencewidget.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class TestQgsRelationReferenceWidget : public QObject
6767
void testAddEntryNoGeom();
6868
void testDependencies(); // Test relation datasource, id etc. config storage
6969
void testSetFilterExpression();
70+
void testSetFilterExpressionWithOrClause();
7071

7172
private:
7273
std::unique_ptr<QgsVectorLayer> mLayer1;
@@ -735,5 +736,42 @@ void TestQgsRelationReferenceWidget::testSetFilterExpression()
735736
QCOMPARE( w.mComboBox->count(), 3 );
736737
}
737738

739+
740+
741+
void TestQgsRelationReferenceWidget::testSetFilterExpressionWithOrClause()
742+
{
743+
744+
// init a relation reference widget
745+
QStringList filterFields = { "material", "diameter", "raccord" };
746+
747+
QWidget parentWidget;
748+
QgsRelationReferenceWidget w( &parentWidget );
749+
750+
QEventLoop loop;
751+
connect( qobject_cast<QgsFeatureFilterModel *>( w.mComboBox->model() ), &QgsFeatureFilterModel::filterJobCompleted, &loop, &QEventLoop::quit );
752+
753+
w.setChainFilters( true );
754+
w.setFilterFields( filterFields );
755+
w.setRelation( *mRelation, true );
756+
w.setFilterExpression( QStringLiteral( " \"raccord\" = 'sleeve' OR FALSE " ) );
757+
w.init();
758+
759+
QStringList items = getComboBoxItems( w.mComboBox );
760+
761+
loop.exec();
762+
763+
// in case there is no filter, the number of filtered features will be 4
764+
QCOMPARE( w.mComboBox->count(), 2 );
765+
766+
QList<QComboBox *> cbs = w.mFilterComboBoxes;
767+
cbs[0]->setCurrentIndex( cbs[0]->findText( "steel" ) );
768+
769+
loop.exec();
770+
771+
QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "NULL" ) );
772+
// in case there is no field filter, the number of filtered features will be 2
773+
QCOMPARE( w.mComboBox->count(), 1 );
774+
}
775+
738776
QGSTEST_MAIN( TestQgsRelationReferenceWidget )
739777
#include "testqgsrelationreferencewidget.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.