@@ -245,24 +245,12 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
245
245
if ( !mReferencedLayer )
246
246
return ;
247
247
248
- QgsFeatureIterator fit;
248
+ QgsAttributes attrs = QgsAttributes ( mReferencingLayer ->pendingFields ().count () );
249
+ attrs[mFkeyFieldIdx ] = value;
249
250
250
- // TODO: Rewrite using expression
251
- if ( mMasterModel )
252
- {
253
- fit = mMasterModel ->layerCache ()->getFeatures ( QgsFeatureRequest () );
254
- }
255
- else
256
- {
257
- fit = mReferencedLayer ->getFeatures ( QgsFeatureRequest () );
258
- }
259
- while ( fit.nextFeature ( mFeature ) )
260
- {
261
- if ( mFeature .attribute ( mFkeyFieldIdx ) == value )
262
- {
263
- break ;
264
- }
265
- }
251
+ QgsFeatureRequest request = mRelation .getReferencedFeatureRequest ( attrs );
252
+
253
+ mReferencedLayer ->getFeatures ( request ).nextFeature ( mFeature );
266
254
267
255
if ( !mFeature .isValid () )
268
256
{
@@ -284,7 +272,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
284
272
}
285
273
else
286
274
{
287
- int i = mComboBox ->findData ( value , QgsAttributeTableModel::FeatureIdRole );
275
+ int i = mComboBox ->findData ( mFeature . id () , QgsAttributeTableModel::FeatureIdRole );
288
276
if ( i == -1 && mAllowNull )
289
277
{
290
278
mComboBox ->setCurrentIndex ( 0 );
@@ -358,10 +346,9 @@ QVariant QgsRelationReferenceWidget::foreignKey()
358
346
}
359
347
else
360
348
{
361
- QVariant varFid = mComboBox ->itemData ( mComboBox ->currentIndex (), QgsAttributeTableModel::FeatureIdRole );
362
- if ( varFid.isNull () )
349
+ if ( !mFeature .isValid () )
363
350
{
364
- return QVariant ();
351
+ return QVariant ( mReferencingLayer -> pendingFields (). at ( mFkeyFieldIdx ). type () );
365
352
}
366
353
else
367
354
{
0 commit comments