Important: Please read the Qt Code of Conduct -

Need help with QgsGraduatedSymbolRendererV2 developing a QGIS plugin PLEASE!!!

  • Hi there,

    I'm new to programming but had to create a QGIS tool as a plugin for a project at work. I wrote this in Python, teaching myself along the way. The tool runs an analysis on a database in Postgres and then imports the layer into GQIS with the calculated values. What I desperately need help with is writing the python scripts in the plugin that will apply graduated symbols to the 'score' attribute column once imported. I've gone over the "Using Vector Layers" section of the PyQGIS documentation thoroughly numerous times but the examples are only for cases using the EqualInterval method of classifying breaks, whereas I need to use the Quantile approach with somewhere between 7 to 10 classes. I've gone through the documentation's instructions and applied what's there to my plugin and it works, but again, only applying EqualInterval breaks and for only 2 classes! What I have from the point of importing the Postgres layer is this:

    #Connect to database and import layer
    uri = QgsDataSourceURI()
    uri.setConnection("localhost", "5432", "public", "postgres", "password")
    uri.setDataSource("susip", str(filename)+"_mce", "the_geom")
    layer = QgsVectorLayer(uri.uri(), str(filename)+"_mce", "postgres")

    #Apply graduated symbology
    fieldName = "score"
    renderer = QgsGraduatedSymbolRendererV2()

    #Add layer to map canvas
    QgsMapLayerRegistry.instance().addMapLayer( layer )

    The result is that I see the layer in the TOC pane on the left of the QGIS window but a blank map canvas. If I go to the layer's properties, I notice that the Symbology method is set to "Graduated", the break method is set to "Quantile", the number of classes is set to "5" and the colour ramp goes from light blue to dark blue. It seems as most of the elements to get a graduated symbol thematic map up are there except that the 'score' column has not be classified. If I click the "classify" button and "ok", then I get a thematic map. I've tried all sorts of commands and viewing the QgsGraduatedSymbolRendererV2 documentation online but I see no commands to classify the desired attribute column or set the colour ramp. The PyQGIS tutorial seems to classify things manually but again, not for the Quantile approach.

    Can someone PLEASE help me finish this code?

    Many thanks,


Log in to reply