0

In PyQGIS, I'm using QgsProject.instance().read(project_file) to open a QGIS project, but when the project contains unavailable layers, the window 'Handle Unavailable Layers' always appears. How to suppress this window with PyQGIS?

2 Answers 2

0

The API provides a way to set a custom bad layer handler which you could use to suppress the 'Handle Unavailable Layers' dialog.

class BadLayerDoNothingHandler(QgsProjectBadLayerHandler):
        def handleBadLayers(self, dom_nodes):
            # skip any bad layer handling by doing nothing
            pass
            
QgsProject.instance().setBadLayerHandler(BadLayerDoNothingHandler())
QgsProject.instance().read(project_file)
1
  • Not sure though on how you would reset it to the standard bad layer handler if thats required. Something like QgsProject().instance().setBadLayerHandler(QgsProjectBadLayerHandler()) does not seem to work.
    – CodeBard
    Commented Jul 31, 2023 at 19:18
0

Good idea. This code works:

class CustomBadLayerHandler(QgsProjectBadLayerHandler):
    def handleBadLayers(self, layerStrings):
        pass

# Set the custom bad layer handler
bad_layer_handler = CustomBadLayerHandler()
QgsProject.instance().setBadLayerHandler(bad_layer_handler)

# Open the project
project = QgsProject.instance()
success = project.read(project_file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.