3

I'm working on a Qgis python module. I would like to apply a symbol on a layer by his name entry from the symbology database. I know that some defaults symbols are saved into a sqlite db (symbology-style.db) in table symbol. I have not found how to apply the style loaded from the db on the layer. Someone know ?

3

In the QGIS Python console, use the following code, comments in the code :

import os

# instance of the QGIS application
app = QgsApplication.instance()
# path to the user default directory symbology db
path_db = os.path.join(
    app.qgisSettingsDirPath(),
    "symbology-style.db"
)
# new QgsStyle object
sty = QgsStyle()
# load the db
sty.load(path_db)
# get a marker symbol by name
marker_symbol = sty.symbol("diamond red")

# active point layer
point_layer = iface.activeLayer()
# set the new marker symbol to the active layer
point_layer.renderer().setSymbol(marker_symbol)
# refresh the symbology displayed in the canvas
iface.mapCanvas().refreshAllLayers()
| improve this answer | |
  • Perfect ! Thanks a lot ! – TrsBar Mar 10 at 16:20

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.