7

Currently, to load a saved style you have to open the layer properties, scroll to the bottom and click on the style tab. As this is a regular thing I need to do when creating new maps I was wondering if I am missing and easier or quicker way to do this? I have the layer styling window open all the time, but this window doesn't have access to the style button I need to load specific styles I regularly need when starting a new map.

Craig
  • 101
  • 1
  • 3
  • Very nice, thank you! But I like to go further. I mostly use the same styles over an over. So I would like to have a button, which apply a certain style to a layer. Like a button for a hillshade which I can apply to my DEMs Can someone tell how to modify the code above to achive this? Thanks in advance Stefan – Stefan Sep 09 '22 at 09:00

1 Answers1

9

We could create a new button on the toolbar which lets you choose a style to apply to the selected layer by writing some code into the Python Console. Open the console using Ctrl + Atl + P and then copy/paste the code below:

action = QAction(QIcon(""), "Load style for selected layer", iface.mainWindow())
action.setCheckable(False)
iface.addToolBarIcon(action)

def loadStyle(layer):
    layer = iface.activeLayer()
    filename, selected_filter = QFileDialog.getOpenFileName(None, "Select style file", "", "QGIS Layer Style File (*.qml *.QML)")
    if filename:
        layer.loadNamedStyle(filename)
        layer.triggerRepaint()

action.triggered.connect(loadStyle)

A new button should be made available on the toolbar. Select the layer you're interested in from the Layers Panel and then click the button which opens up a file browser:

Load style button from toolbar

Note that button will disappear when QGIS is restarted.


Edit:

If you want to avoid typing the code each time you load QGIS, you could consider creating a startup.py script which is executed each time QGIS is loaded. Assuming you are using QGIS 3, create a text file and save it as startup.py in the relevant directory. Use the same code as above but you must include the modules required. So your script should look like:

from qgis.utils import iface
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, QFileDialog

action = QAction(QIcon("C:/Program Files/QGIS 3.8/apps/qgis/icons/qgis-qml.ico"), "Load style for selected layer", iface.mainWindow())
action.setCheckable(False)
iface.addToolBarIcon(action)

def loadStyle(layer):
    layer = iface.activeLayer()
    filename, selected_filter = QFileDialog.getOpenFileName(None, "Select style file", "", "QGIS Layer Style File (*.qml *.QML)")
    if filename:
        layer.loadNamedStyle(filename)
        layer.triggerRepaint()

action.triggered.connect(loadStyle)

New style icon

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • Thanks for this answer. The concept works great while QGIS is running but not sure I want to reload I start QGIS. If I could have saved that button so it would stay they when I restart QGIS it would have been the perfect answer. Nice try though, and I would like to acknowledge your efforts in taking the time to answer my question – Craig Oct 17 '19 at 02:38
  • @Craig - Most welcome, I've edited the post to include a method which uses a startup script so that the code is loaded each time QGIS is restarted without you having to constantly type in the code. Hope this helps! – Joseph Oct 17 '19 at 12:16
  • 2
    @Craig - And for aesthetic purposes, you could use the same icon as the style manager by using action = QAction(QIcon("C:/Program Files/QGIS 3.8/apps/qgis/icons/qgis-qml.ico"), "Load style for selected layer", iface.mainWindow()) (assuming you're using Windows). – Joseph Oct 17 '19 at 12:24
  • 1
    Really appreciate you taking the time to do this for me - I hoped it also helped someone else. This will be very helpful. Thank you – Craig Oct 21 '19 at 00:43
  • @Craig - No problem, glad it helped :) – Joseph Oct 21 '19 at 09:23