Friday, 23 August 2019

qgis - How to set default bad layers handler in the python plugin?


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.


Thanks,



Answer



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 )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...