I am trying to write a python plugin for QGis. My python plugin will copy a template qgis project into data catalog with ESRI shapefiles. Because data catalogs can vary in its content there will be possibility that in the output catalog may be less layers than in the project template.
Is it possible to set default bad layer handler inside python plugin every time when I load programmatically copied project form output catalog?
I am new in programming qgis plugin and I suppose that I should use something like that:

proj = QgsProject.instance()
handler = proj.setBadLayerHandler() #I don't know what kind of arguments I should put in this method.


1 Answer 1


QgsProjectBadLayerHandler is an abstract base class. You have to create a derived class, create an instance from this and pass this instance to the QgsProject. Within your derived class, implement your logic in the handleBadLayers( domNodes, domDocument ) method.

A very short example:

# this is the derived class
class MyBadLayerHandler( QgsProjectBadLayerHandler ):
    def __init__( self ):
        QgsProjectBadLayerHandler.__init__( self )

    # this is the method that will be called to handle bad layers
    def handleBadLayers( self, domNodes, domDocument ):
        print( 'oops, bad layer!' )

# this is your instance of your handler
myBadLayerHandler = MyBadLayerHandler()
# finally assign the handler
QgsProject.instance().setBadLayerHandler( myBadLayerHandler )
  • I have already used myBadLayerHandler(QgsProjectBadLayerDefaultHandler): in the same way as you suggested. I'm not sure, that is a good way. In my plugin it works first time, next time Qgis alwayes crashes. I think this a result of my lack of experience in the advanced programming using an abstract base classes. I will try to do this in the next version of plugin. Current version will use the default qgis prompt gui bad layer handler. Thanks!
    – ConeGIS
    Jul 29, 2013 at 22:28
  • 1
    You should call QgsProjectBadLayerHandler.__init__(self) in def __init__
    – Nathan W
    Jul 29, 2013 at 22:48
  • There is one tiny difference between 1.8 and 2.0: With 1.8 you kept ownership of the bad layer handler. That means, you have to make sure, you keep a reference to it somewhere in your plugin code, so it does not get deleted. This could be the source of your crash. With 2.0 this will not be necessary any more. Jul 30, 2013 at 7:01
  • I use QgsProjectBadLayerDefaultHandler.__init__(self) in def __init__ of plugin. Inside plugin I create derived class for bad layer handler class myBadLayerHandler(QgsProjectBadLayerDefaultHandler): def handleBadLayers(self, domNodes, domDocument ): print( 'bad layer!' ) and after that in code I use self.mBadLayerHandler = pluginDialog.myBadLayerHandler() QgsProject.instance().setBadLayerHandler(self.mBadLayerHandler) It works properly once. Do you have any idea what I should correct
    – ConeGIS
    Jul 30, 2013 at 7:07
  • Which version do you have? Jul 30, 2013 at 7:17

Your Answer

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

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