Skip to content
Permalink
Browse files

Merge pull request #5744 from pblottiere/refrel_crash

[bugfix]  Fixes a crash in QgsRelationReferenceWidgetWrapper
  • Loading branch information
pblottiere committed Nov 28, 2017
2 parents 6584749 + 7ff6470 commit a316530b86e9cc97170abfdcbe72e5314bf7406c
@@ -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();
@@ -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
{
@@ -43,6 +45,7 @@ class TestQgsRelationReferenceWidget : public QObject
void testChainFilter();
void testChainFilterRefreshed();
void testChainFilterDeleteForeignKey();
void testInvalidRelation();

private:
std::unique_ptr<QgsVectorLayer> mLayer1;
@@ -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.
You can’t perform that action at this time.