Skip to content

Commit 56e98f2

Browse files
committedMar 10, 2019
[3d] fix crash when switching terrain generator (fixes #21538)
The problem was introduced in PR #8828 when fixing issue #20963 This fix immediately deletes terrain entity on generator change and informs other code about that + identify map tool is now aware of the fact that terrain entity may be temporarily null.
1 parent 8305c0e commit 56e98f2

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed
 

‎src/3d/qgs3dmapscene.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ void Qgs3DMapScene::onFrameTriggered( float dt )
351351

352352
void Qgs3DMapScene::createTerrain()
353353
{
354+
if ( mTerrain )
355+
{
356+
mChunkEntities.removeOne( mTerrain );
357+
358+
mTerrain->deleteLater();
359+
mTerrain = nullptr;
360+
361+
emit terrainEntityChanged();
362+
}
363+
354364
if ( !mTerrainUpdateScheduled )
355365
{
356366
// defer re-creation of terrain: there may be multiple invocations of this slot, so create the new entity just once
@@ -362,14 +372,6 @@ void Qgs3DMapScene::createTerrain()
362372

363373
void Qgs3DMapScene::createTerrainDeferred()
364374
{
365-
if ( mTerrain )
366-
{
367-
mChunkEntities.removeOne( mTerrain );
368-
369-
mTerrain->deleteLater();
370-
mTerrain = nullptr;
371-
}
372-
373375
double tile0width = mMap.terrainGenerator()->extent().width();
374376
int maxZoomLevel = Qgs3DUtils::maxZoomLevel( tile0width, mMap.mapTileResolution(), mMap.maxTerrainGroundError() );
375377

‎src/3d/qgs3dmapscene.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class _3D_EXPORT Qgs3DMapScene : public Qt3DCore::QEntity
5959

6060
//! Returns camera controller
6161
QgsCameraController *cameraController() { return mCameraController; }
62-
//! Returns terrain entity
62+
//! Returns terrain entity (may be temporarily null)
6363
QgsTerrainEntity *terrainEntity() { return mTerrain; }
6464

6565
//! Resets camera view to show the whole scene (top view)

‎src/app/3d/qgs3dmaptoolidentify.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,20 @@ void Qgs3DMapToolIdentify::mousePressEvent( QMouseEvent *event )
7777

7878
void Qgs3DMapToolIdentify::activate()
7979
{
80-
Qt3DRender::QObjectPicker *picker = mCanvas->scene()->terrainEntity()->terrainPicker();
81-
connect( picker, &Qt3DRender::QObjectPicker::clicked, this, &Qgs3DMapToolIdentify::onTerrainPicked );
80+
if ( QgsTerrainEntity *terrainEntity = mCanvas->scene()->terrainEntity() )
81+
{
82+
connect( terrainEntity->terrainPicker(), &Qt3DRender::QObjectPicker::clicked, this, &Qgs3DMapToolIdentify::onTerrainPicked );
83+
}
8284

8385
mCanvas->scene()->registerPickHandler( mPickHandler.get() );
8486
}
8587

8688
void Qgs3DMapToolIdentify::deactivate()
8789
{
88-
Qt3DRender::QObjectPicker *picker = mCanvas->scene()->terrainEntity()->terrainPicker();
89-
disconnect( picker, &Qt3DRender::QObjectPicker::clicked, this, &Qgs3DMapToolIdentify::onTerrainPicked );
90+
if ( QgsTerrainEntity *terrainEntity = mCanvas->scene()->terrainEntity() )
91+
{
92+
disconnect( terrainEntity->terrainPicker(), &Qt3DRender::QObjectPicker::clicked, this, &Qgs3DMapToolIdentify::onTerrainPicked );
93+
}
9094

9195
mCanvas->scene()->unregisterPickHandler( mPickHandler.get() );
9296
}
@@ -137,6 +141,8 @@ void Qgs3DMapToolIdentify::onTerrainEntityChanged()
137141
{
138142
// no need to disconnect from the previous entity: it has been destroyed
139143
// start listening to the new terrain entity
140-
Qt3DRender::QObjectPicker *picker = mCanvas->scene()->terrainEntity()->terrainPicker();
141-
connect( picker, &Qt3DRender::QObjectPicker::clicked, this, &Qgs3DMapToolIdentify::onTerrainPicked );
144+
if ( QgsTerrainEntity *terrainEntity = mCanvas->scene()->terrainEntity() )
145+
{
146+
connect( terrainEntity->terrainPicker(), &Qt3DRender::QObjectPicker::clicked, this, &Qgs3DMapToolIdentify::onTerrainPicked );
147+
}
142148
}

0 commit comments

Comments
 (0)
Please sign in to comment.