import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface
# set path to your styles here
qml_path = '/home/user/qml'
layer = iface.activeLayer()
style_manager = layer.styleManager()
# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)
for qml_file in [f for f in os.listdir(qml_path)
if os.path.isfile(os.path.join(qml_path, f)) and
f.endswith('.qml')]:
# get style name from file
style_name = os.path.basename(qml_file).strip('.qml')
# add style with new name
style_manager.addStyle(style_name, style)
# set new style as current
style_manager.setCurrentStyle(style_name)
# load qml to current style
(message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
print message
if not success: # if style not loaded remove it
style_manager.removeStyle(style_name)
import os
from qgis.core import QgsMapLayerStyle
from qgis.utils import iface
# set path to your styles here
qml_path = '/home/user/qml'
layer = iface.activeLayer()
style_manager = layer.styleManager()
# read valid style from layer
style = QgsMapLayerStyle()
style.readFromLayer(layer)
for qml_file in [f for f in os.listdir(qml_path)
if os.path.isfile(os.path.join(qml_path, f)) and
f.endswith('.qml')]:
# get style name from file
style_name = os.path.basename(qml_file).strip('.qml')
# add style with new name
style_manager.addStyle(style_name, style)
# set new style as current
style_manager.setCurrentStyle(style_name)
# load qml to current style
(message, success) = layer.loadNamedStyle(os.path.join(qml_path, qml_file))
print message
if not success: # if style not loaded remove it
style_manager.removeStyle(style_name)