@@ -811,7 +811,7 @@ void QgsRelationReferenceWidget::filterChanged()
811
811
{
812
812
QVariant nullValue = QgsApplication::nullRepresentation ();
813
813
814
- QStringList filters;
814
+ QMap<QString, QString> filters;
815
815
QgsAttributeList attrs;
816
816
817
817
QComboBox *scb = qobject_cast<QComboBox *>( sender () );
@@ -822,6 +822,11 @@ void QgsRelationReferenceWidget::filterChanged()
822
822
QgsFeatureIds featureIds;
823
823
QString filterExpression;
824
824
825
+ // comboboxes have to be disabled before building filters
826
+ if ( mChainFilters )
827
+ disableChainedComboBoxes ( scb );
828
+
829
+ // build filters
825
830
Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
826
831
{
827
832
if ( cb->currentIndex () != 0 )
@@ -830,11 +835,11 @@ void QgsRelationReferenceWidget::filterChanged()
830
835
831
836
if ( cb->currentText () == nullValue.toString () )
832
837
{
833
- filters << QStringLiteral ( " \" %1\" IS NULL" ).arg ( fieldName );
838
+ filters[fieldName] = QStringLiteral ( " \" %1\" IS NULL" ).arg ( fieldName );
834
839
}
835
840
else
836
841
{
837
- filters << QgsExpression::createFieldEqualityExpression ( fieldName, cb->currentText () );
842
+ filters[fieldName] = QgsExpression::createFieldEqualityExpression ( fieldName, cb->currentText () );
838
843
}
839
844
attrs << mReferencedLayer ->fields ().lookupField ( fieldName );
840
845
}
@@ -854,12 +859,7 @@ void QgsRelationReferenceWidget::filterChanged()
854
859
continue ;
855
860
}
856
861
857
- if ( ccb->currentIndex () == 0 )
858
- {
859
- cb->setCurrentIndex ( 0 );
860
- cb->setEnabled ( false );
861
- }
862
- else
862
+ if ( ccb->currentIndex () != 0 )
863
863
{
864
864
const QString fieldName = cb->property ( " Field" ).toString ();
865
865
filtered = true ;
@@ -873,9 +873,9 @@ void QgsRelationReferenceWidget::filterChanged()
873
873
QStringList texts;
874
874
Q_FOREACH ( const QString &txt, mFilterCache [ccb->property ( " Field" ).toString ()][ccb->currentText ()] )
875
875
{
876
- QStringList filtersAttrs = filters;
877
- filtersAttrs << QgsExpression::createFieldEqualityExpression ( fieldName, txt );
878
- QString expression = filtersAttrs.join ( QStringLiteral ( " AND " ) );
876
+ QMap<QString, QString> filtersAttrs = filters;
877
+ filtersAttrs[fieldName] = QgsExpression::createFieldEqualityExpression ( fieldName, txt );
878
+ QString expression = filtersAttrs.values (). join ( QStringLiteral ( " AND " ) );
879
879
880
880
QgsAttributeList subset = attrs;
881
881
subset << mReferencedLayer ->fields ().lookupField ( fieldName );
@@ -909,9 +909,13 @@ void QgsRelationReferenceWidget::filterChanged()
909
909
910
910
if ( !mChainFilters || ( mChainFilters && !filtered ) )
911
911
{
912
- filterExpression = filters.join ( QStringLiteral ( " AND " ) );
912
+ filterExpression = filters.values ().join ( QStringLiteral ( " AND " ) );
913
+
914
+ QgsFeatureRequest req = QgsFeatureRequest ().setSubsetOfAttributes ( attrs );
915
+ if ( !filterExpression.isEmpty () )
916
+ req.setFilterExpression ( filterExpression );
913
917
914
- QgsFeatureIterator it ( mMasterModel ->layerCache ()->getFeatures ( QgsFeatureRequest (). setFilterExpression ( filterExpression ). setSubsetOfAttributes ( attrs ) ) );
918
+ QgsFeatureIterator it ( mMasterModel ->layerCache ()->getFeatures ( req ) );
915
919
916
920
while ( it.nextFeature ( f ) )
917
921
{
@@ -951,3 +955,28 @@ void QgsRelationReferenceWidget::updateAddEntryButton()
951
955
mAddEntryButton ->setVisible ( mAllowAddFeatures );
952
956
mAddEntryButton ->setEnabled ( mReferencedLayer && mReferencedLayer ->isEditable () );
953
957
}
958
+
959
+ void QgsRelationReferenceWidget::disableChainedComboBoxes ( const QComboBox *scb )
960
+ {
961
+ QComboBox *ccb = nullptr ;
962
+ Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
963
+ {
964
+ if ( !ccb )
965
+ {
966
+ if ( cb == scb )
967
+ {
968
+ ccb = cb;
969
+ }
970
+
971
+ continue ;
972
+ }
973
+
974
+ if ( ccb->currentIndex () == 0 )
975
+ {
976
+ cb->setCurrentIndex ( 0 );
977
+ cb->setEnabled ( false );
978
+ }
979
+ else
980
+ ccb = cb;
981
+ }
982
+ }
0 commit comments