Skip to content

Commit 2934bcd

Browse files
committed
Fix NULL value for relation reference widget
1 parent 2fedba0 commit 2934bcd

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/gui/editorwidgets/qgsrelationreferencewidget.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ void QgsRelationReferenceWidget::initWidget( QWidget* editor )
7777

7878
if ( relation.isValid() )
7979
{
80+
if ( config( "AllowNULL" ).toBool() )
81+
{
82+
mComboBox->addItem( tr( "(no selection)" ), QVariant( field().type() ) );
83+
}
84+
8085
mReferencedLayer = relation.referencedLayer();
8186
int refFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().first().second );
8287

@@ -95,11 +100,6 @@ void QgsRelationReferenceWidget::initWidget( QWidget* editor )
95100
mFidFkMap.insert( f.id(), f.attribute( refFieldIdx ) );
96101
}
97102

98-
if ( config( "AllowNULL" ).toBool() )
99-
{
100-
mComboBox->addItem( "[NULL]" );
101-
}
102-
103103
// Only connect after iterating, to have only one iterator on the referenced table at once
104104
connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( referenceChanged( int ) ) );
105105
}
@@ -119,7 +119,7 @@ QVariant QgsRelationReferenceWidget::value()
119119
QVariant varFid = mComboBox->itemData( mComboBox->currentIndex() );
120120
if ( varFid.isNull() )
121121
{
122-
return QVariant();
122+
return QVariant( field().type() );
123123
}
124124
else
125125
{
@@ -129,9 +129,24 @@ QVariant QgsRelationReferenceWidget::value()
129129

130130
void QgsRelationReferenceWidget::setValue( const QVariant& value )
131131
{
132-
QgsFeatureId fid = mFidFkMap.key( value );
133132
int oldIdx = mComboBox->currentIndex();
134-
mComboBox->setCurrentIndex( mComboBox->findData( fid ) );
133+
134+
if ( value.isNull() )
135+
{
136+
if ( config( "AllowNULL" ).toBool() )
137+
{
138+
mComboBox->setCurrentIndex( 0 );
139+
}
140+
else
141+
{
142+
mComboBox->setCurrentIndex( -1 );
143+
}
144+
}
145+
else
146+
{
147+
QgsFeatureId fid = mFidFkMap.key( value );
148+
mComboBox->setCurrentIndex( mComboBox->findData( fid ) );
149+
}
135150

136151
if ( !mInitialValueAssigned )
137152
{

src/gui/qgsattributeform.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ bool QgsAttributeForm::save()
155155
QVariant srcVar = eww->value();
156156
if ( dstVar != srcVar && srcVar.isValid() )
157157
{
158-
dst[eww->fieldIdx()] = eww->value();
158+
dst[eww->fieldIdx()] = srcVar;
159159

160160
doUpdate = true;
161161
}

0 commit comments

Comments
 (0)