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?