Skip to content

Commit dbc4f59

Browse files
troopa81m-kuhn
authored andcommittedDec 27, 2021
[ExternalResourceWidget] Fix crash when loading blank after a page
1 parent 54946c9 commit dbc4f59

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
 

‎src/gui/qgsexternalresourcewidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void QgsExternalResourceWidget::loadDocument( const QString &path )
217217
#ifdef WITH_QTWEBKIT
218218
if ( mDocumentViewerContent == Web )
219219
{
220-
mWebView->setUrl( QUrl( QStringLiteral( "about:blank" ) ) );
220+
mWebView->load( QUrl( QStringLiteral( "about:blank" ) ) );
221221
}
222222
#endif
223223
if ( mDocumentViewerContent == Image )

‎src/gui/qgsexternalresourcewidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
171171
QWebView *mWebView = nullptr;
172172
#endif
173173

174+
friend class TestQgsExternalResourceWidgetWrapper;
174175
};
175176

176177
#endif // QGSEXTERNALRESOURCEWIDGET_H

‎tests/src/gui/testqgsexternalresourcewidgetwrapper.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
#include <QLineEdit>
2424

25+
#ifdef WITH_QTWEBKIT
26+
#include <QWebFrame>
27+
#include <QWebView>
28+
#endif
29+
30+
#define SAMPLE_IMAGE QStringLiteral( "%1/sample_image.png" ).arg( TEST_DATA_DIR )
31+
2532
/**
2633
* @ingroup UnitTests
2734
* This is a unit test for the external resource widget wrapper
@@ -37,6 +44,7 @@ class TestQgsExternalResourceWidgetWrapper : public QObject
3744
void init();// will be called before each testfunction is executed.
3845
void cleanup();// will be called after every testfunction.
3946
void test_setNullValues();
47+
void testBlankAfterValue();
4048

4149
private:
4250
std::unique_ptr<QgsVectorLayer> vl;
@@ -63,6 +71,14 @@ void TestQgsExternalResourceWidgetWrapper::init()
6371
vl = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "NoGeometry?field=link:string" ),
6472
QStringLiteral( "myvl" ),
6573
QLatin1String( "memory" ) );
74+
75+
QgsFeature feat1( vl->fields(), 1 );
76+
feat1.setAttribute( QStringLiteral( "type" ), QStringLiteral( "type1" ) );
77+
vl->dataProvider()->addFeature( feat1 );
78+
79+
QgsFeature feat2( vl->fields(), 2 );
80+
feat2.setAttribute( QStringLiteral( "type" ), QStringLiteral( "type2" ) );
81+
vl->dataProvider()->addFeature( feat2 );
6682
}
6783

6884
void TestQgsExternalResourceWidgetWrapper::cleanup()
@@ -99,5 +115,42 @@ void TestQgsExternalResourceWidgetWrapper::test_setNullValues()
99115
delete widget;
100116
}
101117

118+
void TestQgsExternalResourceWidgetWrapper::testBlankAfterValue()
119+
{
120+
// test that application doesn't crash when we set a blank page in web preview
121+
// after an item have been set
122+
123+
QgsExternalResourceWidgetWrapper ww( vl.get(), 0, nullptr, nullptr );
124+
QWidget *widget = ww.createWidget( nullptr );
125+
QVERIFY( widget );
126+
127+
QVariantMap config;
128+
config.insert( QStringLiteral( "DocumentViewer" ), QgsExternalResourceWidget::Web );
129+
ww.setConfig( config );
130+
131+
QgsFeature feat = vl->getFeature( 1 );
132+
QVERIFY( feat.isValid() );
133+
ww.setFeature( feat );
134+
135+
ww.initWidget( widget );
136+
QVERIFY( ww.mQgsWidget );
137+
138+
widget->show();
139+
140+
QEventLoop loop;
141+
connect( ww.mQgsWidget->mWebView, &QWebView::loadFinished, &loop, &QEventLoop::quit );
142+
143+
ww.setValues( QString( "file://%1" ).arg( SAMPLE_IMAGE ), QVariantList() );
144+
145+
QVERIFY( ww.mQgsWidget->mWebView->isVisible() );
146+
147+
loop.exec();
148+
149+
ww.setValues( QString(), QVariantList() );
150+
151+
QVERIFY( ww.mQgsWidget->mWebView->isVisible() );
152+
QCOMPARE( ww.mQgsWidget->mWebView->url().toString(), QStringLiteral( "about:blank" ) );
153+
}
154+
102155
QGSTEST_MAIN( TestQgsExternalResourceWidgetWrapper )
103156
#include "testqgsexternalresourcewidgetwrapper.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.