4

Is there a way to know which internal python functions are used by QGIS?

e.g., when I click on "add vector layer" button I would like to know the functions that are called by QGIS.

It would simplify greatly the automation of tasks with QGIS.

1
  • Great question! Don't how I have managed to survive without this kind of tool. :D Jun 14, 2016 at 16:50

1 Answer 1

5

It sounds like a profiling tool might be of interest. However in the scenario you gave, it's possible that the GUI signal doesn't trigger python code. But this might help with python plugins (which includes processing and plugin manager, which are also written in python)

Luckily python 2.7 comes with a profiler (actually, two - profile and cProfile)

In your QGIS console, try opening up an editor, and using a snippet of pyqgis code (I used some code from another question I saw recently, you can replace with your own code)

from PyQt4.QtGui import QColor
import profile

def dostuff():
    r = iface.activeLayer().rendererV2()
    for s in r.symbols():
        s.symbolLayers()[0].setOutlineColor(QColor('#ffffff'))
    iface.mapCanvas().refresh()
    return True

profile.runctx("dostuff()",globals(),locals())

This gives the following output

425 function calls in 0.004 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 :0(activeLayer)
    1    0.000    0.000    0.000    0.000 :0(mapCanvas)
    1    0.000    0.000    0.000    0.000 :0(refresh)
    1    0.000    0.000    0.000    0.000 :0(rendererV2)
  208    0.000    0.000    0.000    0.000 :0(setOutlineColor)
    1    0.000    0.000    0.000    0.000 :0(setprofile)
  208    0.000    0.000    0.000    0.000 :0(symbolLayers)
    1    0.000    0.000    0.000    0.000 :0(symbols)
    1    0.000    0.000    0.004    0.004 <string>:1(<module>)
    1    0.004    0.004    0.004    0.004 Untitled-3.py:4(dostuff)
    1    0.000    0.000    0.004    0.004 profile:0(dostuff())
    0    0.000             0.000          profile:0(profiler)

This will give at least a hint of the names of methods being called. As a bonus, you can find out which are taking the most time (which is what profiling is really about).

Bear in mind a lot of the the pyqgis methods are bindings onto underlying C++ functions, so this will only go down to the python/C++ boundary.

There are various other ways to format and visualise this data, for that you'll need the docs, as this is not something I have a lot of experience with. Hopefully it's a pointer in the right direction :)

1
  • Awesome. This is what I need. Thx. Jun 14, 2016 at 8:29

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.