How to define border colour for rule-based style using PyQGIS?

Since you want the border color to match the corresponding polygon fill, you can use the data defined property @symbol_color, which returns the fill color for each polygon. Then, for the default polygon symbol (which is the base of your QgsRuleBasedRendererV2) you set its color_border property to @symbol_color:

ddp = QgsDataDefined( True, True, "@symbol_color" ) # active, useExpression, expression
symbol.symbolLayer( 0 ).setDataDefinedProperty( "color_border", ddp )

Your script would become this:

from PyQt4.QtGui import QColor

layer = iface.activeLayer()
style_rules = (
    ('First', 'expression_1', '#dbffdb'),
    ('Second', 'expression_2', '#f0ab64'),
)
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
ddp = QgsDataDefined( True, True, "@symbol_color" )
symbol.symbolLayer( 0 ).setDataDefinedProperty( "color_border", ddp )
renderer = QgsRuleBasedRendererV2(symbol)
root_rule = renderer.rootRule()
for label, expression, color_name in style_rules:
    rule = root_rule.children()[0].clone()
    rule.setLabel(label)
    rule.setFilterExpression(expression)
    rule.symbol().setColor(QColor(color_name))
    root_rule.appendChild(rule)

root_rule.removeChildAt(0)
layer.setRendererV2(renderer)
layer.triggerRepaint()

On the other hand, to set the same border color for all polygons in a Rule based renderer, you would use setBorderColor() in this way (after initializing symbol variable):

symbol.symbolLayer( 0 ).setBorderColor( QColor(255,0,0) )

Just tested it on QGIS v2.14.8. Let me know if it works on your QGIS installation.


The general logic for setting border color is:

# black polygon with red border
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
layer_style = {}
layer_style['color'] = '0, 0, 0'
layer_style['size'] = '2.5'
layer_style['color_border'] = '255, 0, 0'
symbol_layer = QgsSimpleFillSymbolLayerV2.create(layer_style)