3

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.

5

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:

    iface.actionCopyLayerStyle().trigger()
    
  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:

    iface.actionPasteLayerStyle().trigger()
    

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 Dec 9 '16 at 5:52
  • I've added a suggestion in the answer, hope it solves your problem. BTW, is you plugin public? – Germán Carrillo Dec 9 '16 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 Dec 10 '16 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. – Germán Carrillo Jan 30 '17 at 3:02
  • Very nice of you. All I need is more time and make this higher priority. Except of learn how to upload into repository... In the first place I made this plugin because it was driving me crazy QGIS turns all paths into slashes which doesn't work if you copy paste path in other instance of QGIS or file explorer. So for public use I need to add some platform check/settings. Plus custom adjusting of shortcut keys. Option to copy with or without style. Generally I need to add some basic settings dialog. – Miro Jan 30 '17 at 4:22

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.