1

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.

asked Apr 20, 2023 at 23:11
3
  • $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/… Commented 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? Commented Apr 21, 2023 at 8:27
  • Thank you, I edited and (hopefully) clarified my question. Commented Apr 21, 2023 at 10:44

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.