Skip to content

Commit

Permalink
Merge pull request #5744 from pblottiere/refrel_crash
Browse files Browse the repository at this point in the history
[bugfix]  Fixes a crash in QgsRelationReferenceWidgetWrapper
  • Loading branch information
pblottiere authored Nov 28, 2017
2 parents 6584749 + 7ff6470 commit a316530
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget *editor )
mWidget->setAllowAddFeatures( config( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );

const QVariant relationName = config( QStringLiteral( "Relation" ) );
QgsRelation relation = relationName.isValid() ?
QgsProject::instance()->relationManager()->relation( relationName.toString() ) :
layer()->referencingRelations( fieldIdx() )[0];

QgsRelation relation; // invalid relation by default
if ( relationName.isValid() )
relation = QgsProject::instance()->relationManager()->relation( relationName.toString() );
else if ( ! layer()->referencingRelations( fieldIdx() ).isEmpty() )
relation = layer()->referencingRelations( fieldIdx() )[0];

// If this widget is already embedded by the same relation, reduce functionality
const QgsAttributeEditorContext *ctx = &context();
Expand Down
14 changes: 14 additions & 0 deletions tests/src/gui/testqgsrelationreferencewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#include <qgsapplication.h>
#include "qgseditorwidgetwrapper.h"
#include <editorwidgets/qgsrelationreferencewidget.h>
#include <editorwidgets/qgsrelationreferencewidgetwrapper.h>
#include <qgsproject.h>
#include <qgsattributeform.h>
#include <qgsrelationmanager.h>
#include <attributetable/qgsattributetablefiltermodel.h>
#include "qgsfeaturelistcombobox.h"
#include "qgsfeaturefiltermodel.h"
#include "qgsgui.h"
#include "qgsmapcanvas.h"

class TestQgsRelationReferenceWidget : public QObject
{
Expand All @@ -43,6 +45,7 @@ class TestQgsRelationReferenceWidget : public QObject
void testChainFilter();
void testChainFilterRefreshed();
void testChainFilterDeleteForeignKey();
void testInvalidRelation();

private:
std::unique_ptr<QgsVectorLayer> mLayer1;
Expand Down Expand Up @@ -273,5 +276,16 @@ void TestQgsRelationReferenceWidget::testChainFilterDeleteForeignKey()
QCOMPARE( cbs[2]->isEnabled(), false );
}

void TestQgsRelationReferenceWidget::testInvalidRelation()
{
QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsMapCanvas canvas;
QgsRelationReferenceWidget editor( new QWidget() );

// initWidget with an invalid relation
QgsRelationReferenceWidgetWrapper ww( &vl, 10, &editor, &canvas, nullptr, nullptr );
ww.initWidget( nullptr );
}

QGSTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"

0 comments on commit a316530

Please sign in to comment.