Following question which was never answered: https://stackoverflow.com/questions/27140520/how-to-access-qgis-menu-items-using-python

Is there any way to access functions in QGIS menu using python? Particularly in my case copy and paste style but best would be to get some general way / documentation where to find what function / tool is behind menu item and if / how is possible to get to it using python?

In my case because I frequently need to copy paste one or few layers from one project to other I made simple QGIS plugin for it. It can quickly copy layer path to clipboard (also raster/vector type and name from layers panel) and paste layer (using shortcut keys ctrl+shift+c / ctrl+shift+v). It works also between two QGIS windows which is exactly what I needed. Except one thing - it does not copy layer style from project. And because copy paste style is already as part of QGIS I would like to use that as step of copy paste layer without need to try to replicate the functionality in my own code.

1 Answer 1


I'll answer the concrete question of copying/pasting layer styles with PyQGIS:

  1. Select the source layer in the QGIS layer tree either by hand or by code.

  2. Run the following command to copy the style:

  3. Select another layer either by hand or by code (as you mentioned, it could be a layer in another QGIS window).

  4. Run the following command to paste the style:


In general, some QGIS functionality is exposed through iface object. Have a look at the different actions that you could call (trigger) at http://www.qgis.org/api/classQgisInterface.html

For your specific use case, since you seem to be managing the clipboard, I suggest you to do this:

  • Save the layer style into a temporary QML file (using layer.saveNamedStyle( pathToQMLFile )),
  • Copy the path to that file into the clipboard,
  • When pasting, load the style from the QML file path in the clipboard (using layer.loadNamedStyle( pathToQMLFile ) and then layer.triggerRepaint()).
  • Thank you. Now I have conflict. Copy paste style is using clipboard as I do for path of the layer. And merging the path with style into clipboard and then with paste getting the path and updating clipboard only to style doesn't work with paste style. Any idea for that please?
    – Miro
    Commented Dec 9, 2016 at 5:52
  • I've added a suggestion in the answer, hope it solves your problem. BTW, is you plugin public? Commented Dec 9, 2016 at 14:37
  • 1
    Thank you. After some thinking I actually got to this solution myself but good to see I was pn the right path. Plugin is not public yet because I have to learn how to put it into repository. I promise I will soon, most likely as experimental because I guess there is a lot of things to improve.
    – Miro
    Commented Dec 10, 2016 at 0:53
  • Hi @Miro, is there any way we can access your plugin? If you need any help with the plugin publication or with the code itself, please let me know. Commented Jan 30, 2017 at 3:02
  • 1
    Sorry it took so long, here is the plugin repository: github.com/mirouml/CopyLayerPath
    – Miro
    Commented Feb 2, 2017 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.