Skip to content

Commit 2fcdef4

Browse files
committed
Fix crash in relation reference widget
1 parent 739e507 commit 2fcdef4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget* parent )
5959
, mCanvas( NULL )
6060
, mMessageBar( NULL )
6161
, mForeignKey( QVariant() )
62-
, mFkeyFieldIdx( -1 )
62+
, mReferencedFieldIdx( -1 )
6363
, mAllowNull( true )
6464
, mHighlight( NULL )
6565
, mMapTool( NULL )
@@ -199,7 +199,8 @@ void QgsRelationReferenceWidget::setRelation( const QgsRelation& relation, bool
199199
mReferencingLayer = relation.referencingLayer();
200200
mRelationName = relation.name();
201201
mReferencedLayer = relation.referencedLayer();
202-
mFkeyFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().at( 0 ).second );
202+
mReferencedFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().at( 0 ).second );
203+
mReferencingFieldIdx = mReferencingLayer->fieldNameIndex( relation.fieldPairs().at( 0 ).first );
203204

204205
QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed );
205206

@@ -260,7 +261,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
260261
return;
261262
}
262263

263-
mForeignKey = mFeature.attribute( mFkeyFieldIdx );
264+
mForeignKey = mFeature.attribute( mReferencedFieldIdx );
264265

265266
if ( mReadOnlySelector )
266267
{
@@ -273,7 +274,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
273274
QString title = expr.evaluate( &context ).toString();
274275
if ( expr.hasEvalError() )
275276
{
276-
title = mFeature.attribute( mFkeyFieldIdx ).toString();
277+
title = mFeature.attribute( mReferencedFieldIdx ).toString();
277278
}
278279
mLineEdit->setText( title );
279280
}
@@ -355,11 +356,11 @@ QVariant QgsRelationReferenceWidget::foreignKey()
355356
{
356357
if ( !mFeature.isValid() )
357358
{
358-
return QVariant( mReferencingLayer->fields().at( mFkeyFieldIdx ).type() );
359+
return QVariant( mReferencingLayer->fields().at( mReferencingFieldIdx ).type() );
359360
}
360361
else
361362
{
362-
return mFeature.attribute( mFkeyFieldIdx );
363+
return mFeature.attribute( mReferencedFieldIdx );
363364
}
364365
}
365366
}
@@ -675,7 +676,7 @@ void QgsRelationReferenceWidget::comboReferenceChanged( int index )
675676
mReferencedLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ).nextFeature( mFeature );
676677
highlightFeature( mFeature );
677678
updateAttributeEditorFrame( mFeature );
678-
emit foreignKeyChanged( mFeature.attribute( mFkeyFieldIdx ) );
679+
emit foreignKeyChanged( mFeature.attribute( mReferencedFieldIdx ) );
679680
}
680681

681682
void QgsRelationReferenceWidget::updateAttributeEditorFrame( const QgsFeature& feature )
@@ -703,15 +704,15 @@ void QgsRelationReferenceWidget::featureIdentified( const QgsFeature& feature )
703704
QString title = expr.evaluate( &context ).toString();
704705
if ( expr.hasEvalError() )
705706
{
706-
title = feature.attribute( mFkeyFieldIdx ).toString();
707+
title = feature.attribute( mReferencedFieldIdx ).toString();
707708
}
708709
mLineEdit->setText( title );
709-
mForeignKey = feature.attribute( mFkeyFieldIdx );
710+
mForeignKey = feature.attribute( mReferencedFieldIdx );
710711
mFeature = feature;
711712
}
712713
else
713714
{
714-
mComboBox->setCurrentIndex( mComboBox->findData( feature.attribute( mFkeyFieldIdx ), QgsAttributeTableModel::FeatureIdRole ) );
715+
mComboBox->setCurrentIndex( mComboBox->findData( feature.attribute( mReferencedFieldIdx ), QgsAttributeTableModel::FeatureIdRole ) );
715716
mFeature = feature;
716717
}
717718

src/gui/editorwidgets/qgsrelationreferencewidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
147147
QVariant mForeignKey;
148148
QgsFeature mFeature;
149149
// Index of the referenced layer key
150-
int mFkeyFieldIdx;
150+
int mReferencedFieldIdx;
151+
int mReferencingFieldIdx;
151152
bool mAllowNull;
152153
QgsHighlight* mHighlight;
153154
QgsMapToolIdentifyFeature* mMapTool;

0 commit comments

Comments
 (0)