It is very good to have available functions on startup.py, I created one to get the qgs project base name and referencing it as a Python expression on the composer using all the information found and it works well!

What I want to do now, is to use the function not on startup but with the project macros, so it will be available for the users of the .qgs file, which we share in our office as a template.

I wrote the following code on the openProject function macro, based on the code I wrote for startup.py:

def openProject():
  from qgis.utils import qgsfunction
  from qgis.core import *
  from PyQt4.QtCore import *
  prjbaseFileName =  QFileInfo(QgsProject.instance().fileName()).baseName()

But I do not know if this is even possible! how can i pass /use the return value of prjbaseFileName to a label on my composer.

I am using QGis 2.2 Valmiera.

1 Answer 1


I think user defined functions should do the trick. The concept is well explained here: http://nathanw.net/2012/11/10/user-defined-expression-functions-for-qgis/

from qgis.utils import qgsfunction
from qgis.core import QGis

@qgsfunction(0, "Python")
def custom_function(values, feature, parent):
    return "hello world"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.