Skip to main content
edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Iterating over line features to change colors using PyQGIS

I want to iterate over line features to change color of each feature to the color I choose. So my file has 7 features, I want am writing the code below, but it does not seem to work.

line_feats = [f for f in line_layer.getFeatures()]
clr = ["blue","red","green","yellow","magenta","cyan","black","red"]
for l in line_feats:
    line_layer.select(l.id())
    iface.mapCanvas().setSelectionColor( QColor(clr[l.id()]))
line_layer.triggerRepaint()

I have tried printing l.id() and it goes from0from 0 to 7, so indexing is not the issue. The thing is, it does not change the color individually, rather it changes for the entire file.

Iterating over line features to change colors

I want to iterate over line features to change color of each feature to the color I choose. So my file has 7 features, I want am writing the code below, but it does not seem to work.

line_feats = [f for f in line_layer.getFeatures()]
clr = ["blue","red","green","yellow","magenta","cyan","black","red"]
for l in line_feats:
    line_layer.select(l.id())
    iface.mapCanvas().setSelectionColor( QColor(clr[l.id()]))
line_layer.triggerRepaint()

I have tried printing l.id() and it goes from0 to 7, so indexing is not the issue. The thing is, it does not change the color individually, rather it changes for the entire file.

Iterating over line features to change colors using PyQGIS

I want to iterate over line features to change color of each feature to the color I choose. So my file has 7 features, I want am writing the code below, but it does not seem to work.

line_feats = [f for f in line_layer.getFeatures()]
clr = ["blue","red","green","yellow","magenta","cyan","black","red"]
for l in line_feats:
    line_layer.select(l.id())
    iface.mapCanvas().setSelectionColor( QColor(clr[l.id()]))
line_layer.triggerRepaint()

I have tried printing l.id() and it goes from 0 to 7, so indexing is not the issue. The thing is, it does not change the color individually, rather it changes for the entire file.

English usage
Source Link
Vince
  • 20.3k
  • 16
  • 48
  • 65

I want to iterate over line features to change color of each feature to the color iI choose. So my file has 7 features, iI want am wiritngwriting the code below, but it does not seem to work.

line_feats = [f for f in line_layer.getFeatures()]
clr = ["blue","red","green","yellow","magenta","cyan","black","red"]
for l in line_feats:
    line_layer.select(l.id())
    iface.mapCanvas().setSelectionColor( QColor(clr[l.id()]))
line_layer.triggerRepaint()

I have tried printing l.id() and it goes from0 to 7, so indexing is not the issue. The thing is, it does not change the color individually, rather it changes for the entire file.

I want to iterate over line features to change color of each feature to the color i choose. So my file has 7 features, i want am wiritng the code below, but it does not seem to work.

line_feats = [f for f in line_layer.getFeatures()]
clr = ["blue","red","green","yellow","magenta","cyan","black","red"]
for l in line_feats:
    line_layer.select(l.id())
    iface.mapCanvas().setSelectionColor( QColor(clr[l.id()]))
line_layer.triggerRepaint()

I have tried printing l.id() and it goes from0 to 7, so indexing is not the issue. The thing is, it does not change the color individually, rather it changes for the entire file.

I want to iterate over line features to change color of each feature to the color I choose. So my file has 7 features, I want am writing the code below, but it does not seem to work.

line_feats = [f for f in line_layer.getFeatures()]
clr = ["blue","red","green","yellow","magenta","cyan","black","red"]
for l in line_feats:
    line_layer.select(l.id())
    iface.mapCanvas().setSelectionColor( QColor(clr[l.id()]))
line_layer.triggerRepaint()

I have tried printing l.id() and it goes from0 to 7, so indexing is not the issue. The thing is, it does not change the color individually, rather it changes for the entire file.

edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Source Link
Loading