Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 newbie developing plugins for QGIS using Python, this is the first one and it is very frustrating hehe. Ok, this is the situation, I have a 1 point layer in QGIS. Its attribute table has 4 fields and 1 row:


1     1     20  "Green"

What I'm trying to do with my plugin is to get the string data from "Estate" field and check the string data:

if string is "Green", the one point layer symbology will turn to green.

if string is not "Green", the one point layer symbology will turn to red.

The last part of this plugin is to refresh every X seconds to check "Estate" field, and change the symbology of this point layer if "Estate" field has changed.

This is what I have done so far in my python script, I put from the run method:

# run method that performs all the real work
def run(self):

    # create and show the dialog
    dlg = PlayMonitoringDialog() 
    # show the dialog
    result = dlg.exec_() 
    # See if OK was pressed
    if result == 1: 
      # do something useful (delete the line containing pass and
      # substitute with your code
      def h():

        layer = self.iface.activeLayer()
        Estado = layer.attributeDisplayName(3)
        if Estado == "green":
          props = { 'color' : '0,255,0' }
          sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleMarker").createSymbolLayer(props)
          s = QgsMarkerSymbolV2([sl])
          props = { 'color' : '255,0,0' }
          sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleMarker").createSymbolLayer(props)
          s = QgsMarkerSymbolV2([sl])
        if hasattr(layer, "setCacheImage"): layer.setCacheImage(None)

  def lanza():

It doesn't work :(, so I hope someone can help me. I have seen the small examples in, I have checked and I have looked for examples in the Internet, but I still don't know how to solve the problems in my script. Thank you very much in advance and sorry for my english, I haven't practice for a while.


share|improve this question

It looks as though your problem is where your functions are defined. You should define all your functions at the beginning of your script, after the import, rather than inside of another function. If you need to use a function inside another, define it first and then call it within the second function. For example, you call lanza() in your first function, but you don't define it until afterwards. Rearranging your functions would take care of the basic Python problems. Any other problems would have to be answered by someone with more QGIS Python experience than I have :).

share|improve this answer
Thank you very much Jay!. This is the kind of things that help me a lot. I'm going to define all the functions as you say and try to call them in the run method. – Lucas Mar 21 '13 at 14:28

Im not a Python programer but should the line:

Estado = layer.attributeDisplayName(3)


Estado = layer.attributeDisplayName(4)

as the attribute you are actually checking is the fourth column not the third?


share|improve this answer
Nigel-- Python starts counting from 0, so the fourth item would have the count 3. – Jay Guarneri Mar 21 '13 at 14:26
Thanks Jay, I learn something useful everyday :) – nigellaw Mar 21 '13 at 16:10

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.