Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm really new to QGIS coding and I'm having a hard time finding (especially) the api classes/functions to carry out actions of the Query Builder window.

I'm basically trying to do this:

  1. Iterate from n=1 to 200...

  2. Query layerA where field_id = n

  3. Query layerB where field_id = n

  4. Query layerC where field_id = n

  5. Save as img_n.png

Pretty simple, I think, but it has become a real pain to do by hand in the GUI.

share|improve this question

Well, on my own, I finally dug out the key function I needed: setSubsetString(). Whoever named this thing?...doesn't seem very intuitive as to its function.

Here's the skeleton I'm working with now:

canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
for n in range(1, 201)
    for layer in allLayers:
        layer.setSubsetString('field_id = ' + str(n))
    canvas.saveAsImage('img_' + str(n) + '.png')

share|improve this answer
setSubString filters, using the providers syntax, the lyaer. It is fine to use it but be aware the syntax might change if you change the layer type you are using. – Nathan W Feb 1 '13 at 0:45
If people want to use this code more generically than I do, yes, they should check that the layer is type vector (QgsMapLayer::LayerType) and probably more checks along the way. – Kory Roberts Feb 1 '13 at 21:22

I would suggest you to go through their API documentation.

Coming back to your question, you can iterate through all layers in the TOC using:

canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
for i in allLayers:
   #Do other stuff here.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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