2

I've made a bit of a plugin to handle badlayers (I'm aware that Change datasource plugin had something for this but the code appears to have been removed now?). I've followed the advice provided in How to set default bad layers handler in the python plugin? and I can get the handler to start when layers are missing.

I'm currently having trouble setting the new data provider. Currently I'm doing

handler = QgsProjectbadLayerHandler()

newPath = "C:\pathtomissinglayer"

handler.setDataSource(domNode,newPath)

where domNode has been passed in from handleBadLayers(self, domNodes)

This doesn't work as expected. The layers still disappear and setting the data source doesn't seem to change anything even though it executes properly. Should I be passing in the newPath as a string or something else? And how do I stop the layers from disappearing?

Edit: There appears to be a new class as a part of 3.6?? called QgsStoreBadLayerInfo. Description says it stores layer IDs of bad layers. The ID could be useful to get the position of the map layer in the layer explorer and then just create a new layer with the correct file path. As is extends from QgsProjectBadLayerHandler could this class just be used in place of QgsProjectBadLayerHandler? Will it be called when layers are missing? I tried but couldn't get it too work

1

Perhaps a bit late, but for reference:

The setDataSource method of QgsProjectBadLayerHandler seem to only update the passed QDOMNode instance. To actually load the layer from the new data source, call readLayer method of QgsProject, passing the same QDOMNode instance that was passed to setDataSource.

Eg. after the approach that OP outlined, call

QgsProject.instance().readLayer(domNode)

in the plugin code.

There is also a short refenence to this in the QGIS Python API Documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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