I use the action editor in QGIS to code an action, but I want to move the code to a PyQGIS script and import it to the action instead of directly typing it in the action editor.
I changed this line
lyr = QgsProject.instance().mapLayer('[% @layer_id %]')
to
lyr = iface.activeLayer()
and I hope that this is correct (when importing iface
). But how to access the clicked feature for the action? How does the following line need to be changed in order to remove the syntax that does not work outside the action editor window?
ft = lyr.getFeature([% $id %])
At least this last line is only working when typed directly in the editor window, see Importing .py script to action editor using QGIS.
-
$id will be the features id, the user clicked on. To get that, you need to use Qt's signal&slot and trigger the action from your script. I would stay with your layer action, the docs have an example to write python code for a layer action: docs.qgis.org/3.28/en/docs/training_manual/create_vector_data/…Andreas Müller– Andreas Müller2023年04月21日 06:18:17 +00:00Commented Apr 21, 2023 at 6:18
-
by the way, what do you mean by "external script"? You can't use iface outside of qgis and you can't get the result of a user interaction from outside. So, what is your goal of your efforts?Andreas Müller– Andreas Müller2023年04月21日 08:27:40 +00:00Commented Apr 21, 2023 at 8:27
-
Thank you, I edited and (hopefully) clarified my question.winnewoerp– winnewoerp2023年04月21日 10:44:33 +00:00Commented Apr 21, 2023 at 10:44