TransWikia.com

Apply style to a layer in a geopackage using PyQGIS

Geographic Information Systems Asked on December 3, 2021

I have a geopackage containing layers and styles stored, I need to assign one of the layers a style defined by its name. I have seen several examples but they use styles stored in external files and I could not solve that task. This is the code that I am trying to use without success.

def TestStyle_gpk(self):
        GpName = 'd:\temp\3584-I-a.gpkg'
        fileName = GpName
        sub_vlayer = QgsVectorLayer(fileName, 'embalses_areal', 'ogr')
        style_manager = sub_vlayer.styleManager()
        styles = sub_vlayer.listStylesInDatabase()
        style_xml = QgsMapLayerStyle(sub_vlayer.getStyleFromDatabase("4")[0])

        style_manager.addStyle('embalses_areal', style_xml)
        style_manager.setCurrentStyle(style_manager)
        style_manager.setCurrentStyle(style_xml)
        self.iface.layerTreeView().refreshLayerSymbology(sub_vlayer.id())
        self.iface.mapCanvas().refreshAllLayers()

And this is the error that QGIS gives:

2020-04-16T18:20:41     WARNING    Traceback (most recent call last):
              File "C:/Users/frank/AppData/Roaming/QGIS/QGIS3profilesMapificacion/python/pluginsgestion_proyectosgestion_proyectos.py", line 270, in TestStyle_gpk
              style_manager.setCurrentStyle(style_manager)
             TypeError: QgsMapLayerStyleManager.setCurrentStyle(): argument 1 has unexpected type 'QgsMapLayerStyleManager'

How can I solve this problem?

One Answer

you need to use

sub_vlayer.saveStyleToDatabase(name="name_style",description="example", useAsDefault=True, uiFileContent="")

Answered by Angel Rene Herrera Calzada on December 3, 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