Skip to content

Commit e08130d

Browse files
committedMar 8, 2016
Merge pull request #2827 from SebDieBln/AllowNullInRelationRef
[Bugfix] offer NULL value in relation reference widget if allowed (fixes #14162)
2 parents c6ebb8c + cc19a57 commit e08130d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed
 

‎src/gui/attributetable/qgsfeaturelistmodel.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
6161
{
6262
return QSettings().value( "qgis/nullValue", "NULL" ).toString();
6363
}
64-
else
64+
else if ( role == QgsAttributeTableModel::FeatureIdRole )
6565
{
6666
return QVariant( QVariant::Int );
6767
}
68+
else
69+
{
70+
return QVariant( QVariant::Invalid );
71+
}
6872
}
6973

7074
if ( role == Qt::DisplayRole || role == Qt::EditRole )
@@ -122,7 +126,14 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
122126

123127
Qt::ItemFlags QgsFeatureListModel::flags( const QModelIndex &index ) const
124128
{
125-
return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
129+
if ( mInjectNull && index.row() == 0 )
130+
{
131+
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
132+
}
133+
else
134+
{
135+
return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
136+
}
126137
}
127138

128139
void QgsFeatureListModel::setInjectNull( bool injectNull )

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ void QgsRelationReferenceWidget::setRelationEditable( bool editable )
234234

235235
void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
236236
{
237-
if ( !value.isValid() || value.isNull() )
237+
if ( !value.isValid() )
238+
{
239+
return;
240+
}
241+
if ( value.isNull() )
238242
{
239243
deleteForeignKey();
240244
return;
@@ -254,7 +258,6 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
254258

255259
if ( !mFeature.isValid() )
256260
{
257-
deleteForeignKey();
258261
return;
259262
}
260263

@@ -535,7 +538,8 @@ void QgsRelationReferenceWidget::init()
535538
}
536539
}
537540

538-
mComboBox->setCurrentIndex( mComboBox->findData( mFeature.id(), QgsAttributeTableModel::FeatureIdRole ) );
541+
QVariant featId = mFeature.isValid() ? mFeature.id() : QVariant( QVariant::Int );
542+
mComboBox->setCurrentIndex( mComboBox->findData( featId, QgsAttributeTableModel::FeatureIdRole ) );
539543

540544
// Only connect after iterating, to have only one iterator on the referenced table at once
541545
connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( comboReferenceChanged( int ) ) );

0 commit comments

Comments
 (0)
Please sign in to comment.