TransWikia.com

Reloading layer in Layers Panel with Python after modifying it in DB Manager

Geographic Information Systems Asked on August 5, 2021

I often have to reload layers in the layers panel after doing changes in the DB-Manager (add a column, delete a default value, …). At the moment I use the changeDatasource plugin. Can this also be done using Python?

3 Answers

I've read several answers for this specific question; none of them seems to work. The only effective solution I could find is:

layer.setDataSource( layer.source(), layer.name(), layer.providerType() )

Tested in QGIS v2.14.8.

Correct answer by Germán Carrillo on August 5, 2021

if you have your layer as a variable, simply layer.reload()

You can obtain a list of all layers of the current project from QgsMapLayerRegistry.instance().mapLayers()

If you don’t care about reloading all other layers at the same time, the one-liner

QgsMapLayerRegistry.instance().reloadAllLayers()

does the job, too.

References:

Answered by christoph on August 5, 2021

QgsProject.instance().reloadAllLayers() worked for me on QGIS 3.10.

Answered by Romário Carvalho Neto on August 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP