Skip to content

Commit

Permalink
More thread safety in expression project access
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 13, 2022
1 parent 52b45b5 commit 2242f64
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/core/expression/qgsexpressionutils.cpp
Expand Up @@ -69,15 +69,23 @@ QgsMapLayer *QgsExpressionUtils::getMapLayer( const QVariant &value, const QgsEx
return ml;

// last resort - QgsProject instance. This is bad, we need to remove this!
QgsProject *project = QgsProject::instance();
auto getMapLayerFromProjectInstance = [ value, &ml ]
{
QgsProject *project = QgsProject::instance();

// No pointer yet, maybe it's a layer id?
ml = project->mapLayer( value.toString() );
if ( ml )
return ml;
// No pointer yet, maybe it's a layer id?
ml = project->mapLayer( value.toString() );
if ( ml )
return;

// Still nothing? Check for layer name
ml = project->mapLayersByName( value.toString() ).value( 0 );
// Still nothing? Check for layer name
ml = project->mapLayersByName( value.toString() ).value( 0 );
};

if ( QThread::currentThread() == qApp->thread() )
getMapLayerFromProjectInstance();
else
QMetaObject::invokeMethod( qApp, getMapLayerFromProjectInstance, Qt::BlockingQueuedConnection );

return ml;
}
Expand Down

0 comments on commit 2242f64

Please sign in to comment.