3

I am trying to build a Qgis plugin which involves setting shortcuts to perform some function on the selected layer in the plugin. When I use my plugin for the first time, everything works as expected. The problem appears when starting the plugin again to change the layer. Nothing works, also when selecting the same layer again.

To help me debugging my problem I wrote a small example script:

active_layer = iface.activeLayer()

def current_layer():
   print('active_layer: '+ str(active_layer))

shortcut = QShortcut(QKeySequence(Qt.Key_Space), iface.mapCanvas())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(current_layer) 

When I just run the first line, I can change the 'active_layer' I print when pressing space_bar. But when I run this entire script again, it's not working anymore. So it has to be smth. with the shortcut being defined again.

I tried shortcut.activated.disconnect(current_layer) and running the script again, with not much success.

Any Idea what I would need to add/change to make this script work when I run it again?

1

I understand your issue but within a plugin, it will not be an issue in fact. To avoid reinstanciating the GUI, there is already a mecanism to run some part of the code once.

You will find below screenshots to position your sample code (PS: I've made some modifications e.g replacing iface with self.iface due to plugin context) and checking it works. My sample for demonstration has been generated with the name Whatever, so the screenshot below are from the file what_ever.py from the root of the plugin. Find the same file but with the name you used.

Add import

Missing imports

Initialize shortcut as an attribute of the instance

shortcut as an attribute

Wrap your code in the block if self.first_start == True: to run it once and avoid the mentioned issue

Run once shortcut creation

3
  • I also have this issue, but I would like to be able to change the selected layer through the dialog when I run it again by getting the layer from a combo box: self.layer1 = self.dlg.mMapLayerComboBox_1.currentLayer(). I dont think that is possible if you put everything within the if self.first_start == True: block? – staf May 29 '20 at 7:24
  • Don't think so too. This answer was just an illustration how to solve the issue not about best practices as I do not know the overall logic from the people asking (their requirements and intents to do the things they want/need) – ThomasG77 May 29 '20 at 11:59
  • Yes, thankyou anyway for your generally helpful answer. I was just asking so as to be sure to not duplicate questions, if I was going to put a new question up. Your answer helped to half solve the problem in my case: I am now defining the shortcuts in this if self.first_start == True: block. But I connect them to my layers later in the code. Thankyou! – staf May 30 '20 at 6:29

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.