@@ -77,6 +77,11 @@ void QgsRelationReferenceWidget::initWidget( QWidget* editor )
77
77
78
78
if ( relation .isValid () )
79
79
{
80
+ if ( config ( " AllowNULL" ).toBool () )
81
+ {
82
+ mComboBox ->addItem ( tr ( " (no selection)" ), QVariant ( field ().type () ) );
83
+ }
84
+
80
85
mReferencedLayer = relation .referencedLayer ();
81
86
int refFieldIdx = mReferencedLayer ->fieldNameIndex ( relation .fieldPairs ().first ().second );
82
87
@@ -95,11 +100,6 @@ void QgsRelationReferenceWidget::initWidget( QWidget* editor )
95
100
mFidFkMap .insert ( f.id (), f.attribute ( refFieldIdx ) );
96
101
}
97
102
98
- if ( config ( " AllowNULL" ).toBool () )
99
- {
100
- mComboBox ->addItem ( " [NULL]" );
101
- }
102
-
103
103
// Only connect after iterating, to have only one iterator on the referenced table at once
104
104
connect ( mComboBox , SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( referenceChanged ( int ) ) );
105
105
}
@@ -119,7 +119,7 @@ QVariant QgsRelationReferenceWidget::value()
119
119
QVariant varFid = mComboBox ->itemData ( mComboBox ->currentIndex () );
120
120
if ( varFid.isNull () )
121
121
{
122
- return QVariant ();
122
+ return QVariant ( field (). type () );
123
123
}
124
124
else
125
125
{
@@ -129,9 +129,24 @@ QVariant QgsRelationReferenceWidget::value()
129
129
130
130
void QgsRelationReferenceWidget::setValue ( const QVariant& value )
131
131
{
132
- QgsFeatureId fid = mFidFkMap .key ( value );
133
132
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
+ }
135
150
136
151
if ( !mInitialValueAssigned )
137
152
{
0 commit comments