4

Is it possible to define a custom Python Expression function like in the project macros?

Each time when I try to enable the macros and write any code, I get a security warning saying that Python macros cannot currently be run (as in the attached pictures).

enter image description here

enter image description here

I get a crash when attempting to run the last screenshot scenario.

Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
asked Nov 29, 2021 at 11:01
2
  • 1
    On the right of the security warning there is an "Enable Macro" link did you try to click on that ? Also on the QGIS option (General tab) what is the status of the "Enable macros" setting ? Commented Nov 29, 2021 at 11:14
  • In the first case ("Hello I am a macro" example) there is an error QgsMessangerBar.INFO . Once I deleted it the macro will work but the warning it will still appear. Whereas in the second case (with that function), the QGIS crashes when I click on "Enable Macro" link. The Enable macros is set to "Ask". Should it be set to "Always"? What I am actually trying to do is to define in the macros a "Custom Python expression function" like to use it in the forms of the project. Commented Nov 29, 2021 at 11:52

1 Answer 1

7

You can create a custom expression function in a macro in this way:

enter image description here

Sample code:

from qgis.utils import qgsfunction
from qgis.core import QgsExpression
@qgsfunction(args='auto', group='test', referenced_columns=[])
def my_sum(value1, value2, feature, parent):
 return value1 + value2
def openProject():
 pass
def saveProject():
 pass
def closeProject():
 QgsExpression.unregisterFunction('my_sum')

Note we unregister the created function when closing the project.

Therefore, the function my_sum() will only be available while your project is open, if the user accepts macros in her/his QGIS session.


Due to security issues that macros could trigger, your users will be asked for confirmation on running your macro, unless they have set the Enable macros option to Always (Not Recommended) in Settings --> Options... --> General.

answered Nov 29, 2021 at 17:12
4
  • Is the function automatically registered? Commented Nov 29, 2021 at 19:57
  • Yes, as soon as QGIS finds the @qgsfunction decorator, QGIS knows that's a function ready to be registered, and proceeds with its registration. Commented Nov 29, 2021 at 20:33
  • Thank you! It works indeed. I did not find this in the documentation. I wonder how you know about it. Commented Dec 6, 2021 at 12:26
  • 1
    Well, there are lots of things you know by practicing and by attempting alternate solutions. I've been working with PyQGIS since 2009! So, that's the secret :) Commented Dec 10, 2021 at 0:03

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.