Skip to content

Commit ca80fb6

Browse files
authored
Merge pull request #9653 from elpaso/bugfix-badlayers-apply-no-style
Fix bad layers style loss when "apply"
2 parents 41f3ac0 + 94b9d8f commit ca80fb6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/app/qgshandlebadlayers.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,22 @@ void QgsHandleBadLayers::apply()
412412
if ( QgsProject::instance()->mapLayer( layerId ) )
413413
{
414414
QgsMapLayer *mapLayer = QgsProject::instance()->mapLayer( layerId );
415-
if ( mapLayer )
415+
QgsDataProvider::ProviderOptions options;
416+
const auto absolutePath { QgsProject::instance()->pathResolver().readPath( datasource ) };
417+
mapLayer->setDataSource( absolutePath, name, provider, options );
418+
dataSourceFixed = mapLayer->isValid();
419+
if ( dataSourceFixed )
416420
{
417-
QgsDataProvider::ProviderOptions options;
418-
mapLayer->setDataSource( datasource, name, provider, options );
419-
dataSourceFixed = mapLayer->isValid();
421+
QString errorMsg;
422+
QgsReadWriteContext context;
423+
context.setPathResolver( QgsProject::instance()->pathResolver() );
424+
context.setProjectTranslator( QgsProject::instance() );
425+
if ( ! mapLayer->readSymbology( node, errorMsg, context ) )
426+
{
427+
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from node XML for layer %1: %2" )
428+
.arg( mapLayer->name( ) )
429+
.arg( errorMsg ) );
430+
}
420431
}
421432
}
422433

0 commit comments

Comments
 (0)