@@ -364,7 +364,7 @@ QVariant QgsRelationReferenceWidget::foreignKey()
364
364
}
365
365
else
366
366
{
367
- return mFidFkMap . value ( varFid. value <QgsFeatureId>() );
367
+ return mFeature . attribute ( mFkeyFieldIdx );
368
368
}
369
369
}
370
370
}
@@ -510,8 +510,12 @@ void QgsRelationReferenceWidget::init()
510
510
mFeatureListModel ->setInjectNull ( mAllowNull );
511
511
if ( mOrderByValue )
512
512
{
513
- int sortIdx = mReferencedLayer ->fieldNameIndex ( QgsExpression ( mReferencedLayer ->displayExpression () ).referencedColumns ().first () );
514
- mFilterModel ->sort ( sortIdx );
513
+ const QStringList referencedColumns = QgsExpression ( mReferencedLayer ->displayExpression () ).referencedColumns ();
514
+ if ( referencedColumns.size () > 0 )
515
+ {
516
+ int sortIdx = mReferencedLayer ->fieldNameIndex ( referencedColumns.first () );
517
+ mFilterModel ->sort ( sortIdx );
518
+ }
515
519
}
516
520
517
521
mComboBox ->setModel ( mFeatureListModel );
@@ -531,7 +535,7 @@ void QgsRelationReferenceWidget::init()
531
535
mComboBox ->setCurrentIndex ( mComboBox ->findData ( mFeature .id (), QgsAttributeTableModel::FeatureIdRole ) );
532
536
533
537
// Only connect after iterating, to have only one iterator on the referenced table at once
534
- connect ( mComboBox , SIGNAL ( activated ( int ) ), this , SLOT ( comboReferenceChanged ( int ) ) );
538
+ connect ( mComboBox , SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( comboReferenceChanged ( int ) ) );
535
539
QApplication::restoreOverrideCursor ();
536
540
}
537
541
}
@@ -671,12 +675,11 @@ void QgsRelationReferenceWidget::mapIdentification()
671
675
672
676
void QgsRelationReferenceWidget::comboReferenceChanged ( int index )
673
677
{
674
- QgsFeatureId fid = mComboBox ->itemData ( index ).value <QgsFeatureId>();
675
- QgsFeature feat;
676
- mReferencedLayer ->getFeatures ( QgsFeatureRequest ().setFilterFid ( fid ) ).nextFeature ( feat );
677
- highlightFeature ( feat );
678
- updateAttributeEditorFrame ( feat );
679
- emit foreignKeyChanged ( mFidFkMap .value ( fid ) );
678
+ QgsFeatureId fid = mComboBox ->itemData ( index , QgsAttributeTableModel::FeatureIdRole ).value <QgsFeatureId>();
679
+ mReferencedLayer ->getFeatures ( QgsFeatureRequest ().setFilterFid ( fid ) ).nextFeature ( mFeature );
680
+ highlightFeature ( mFeature );
681
+ updateAttributeEditorFrame ( mFeature );
682
+ emit foreignKeyChanged ( mFeature .attribute ( mFkeyFieldIdx ) );
680
683
}
681
684
682
685
void QgsRelationReferenceWidget::updateAttributeEditorFrame ( const QgsFeature feature )
@@ -708,6 +711,7 @@ void QgsRelationReferenceWidget::featureIdentified( const QgsFeature& feature )
708
711
else
709
712
{
710
713
mComboBox ->setCurrentIndex ( mComboBox ->findData ( feature.attribute ( mFkeyFieldIdx ), QgsAttributeTableModel::FeatureIdRole ) );
714
+ mFeature = feature;
711
715
}
712
716
713
717
mRemoveFKButton ->setEnabled ( mIsEditable );
@@ -802,8 +806,6 @@ void QgsRelationReferenceWidget::filterChanged()
802
806
{
803
807
const QString fieldName = cb->property ( " Field" ).toString ();
804
808
805
- cb->itemData ( cb->currentIndex () );
806
-
807
809
if ( cb->currentText () == nullValue.toString () )
808
810
{
809
811
filters << QString ( " \" %1\" IS NULL" ).arg ( fieldName );
0 commit comments