Hoping I have understood well your issue, you may set the text from a specified field as Legend by directly creating the categorized style.
Assuming to start from this self-explanatory Attribute Table:
you may assign the "DSCR_UCF"
field as Legend for the "ID_UCF"
values using the following code from the Python Console:
from PyQt4.QtGui import *
import random
layer = iface.activeLayer()
my_dict = {}
for feat in layer.getFeatures(): #iterate over the features for defining the lookup: value -> (color, label)
if feat["ID_UCF"] not in my_dict.keys():
color = QColor.fromRgb(random.randint(0,255),random.randint(0,255),random.randint(0,255)) #assign a random color
my_dict[feat["ID_UCF"]] = (color.name(), feat["DSCR_UCF"]) #lookup: value -> (color, label)
else: # go to the next feature since the label is already stored
continue
# Create a category for each item in my_dict
categories = []
for value, (color_name, label) in my_dict.items():
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
symbol.setColor(QColor(color_name))
category = QgsRendererCategoryV2(value, symbol, label)
categories.append(category)
# Create the render and assign it to the layer
renderer = QgsCategorizedSymbolRendererV2("ID_UCF", categories)
layer.setRendererV2(renderer)
layer.triggerRepaint()
You will get this result:
EDIT
If you want to keep the original colors unchanged, you may run the following code (assuming you previously assigned a custom categorized renderer with custom colors):
from PyQt4.QtGui import *
layer = iface.activeLayer()
f_index = layer.dataProvider().fieldNameIndex('ID_UCF')
unique_values = layer.uniqueValues(f_index)
rend = layer.rendererV2()
colors = [symb.color().name() for symb in rend.symbols()]
diz = dict(zip(unique_values, colors))
my_dict = {}
for feat in layer.getFeatures(): #iterate over the features for defining the lookup: value -> (color, label)
if feat["ID_UCF"] not in my_dict.keys():
val = [diz[feat["ID_UCF"]]]
val.append(feat["DSCR_UCF"])
my_dict[feat["ID_UCF"]] = tuple(val) #lookup: value -> (color, label)
else: # go to the next feature since the label is already stored
continue
# Create a category for each item in my_dict
categories = []
for value, (color_name, label) in sorted(my_dict.items()):
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
symbol.setColor(QColor(color_name))
category = QgsRendererCategoryV2(value, symbol, label)
categories.append(category)
# Create the render and assign it to the layer
renderer = QgsCategorizedSymbolRendererV2("ID_UCF", categories)
layer.setRendererV2(renderer)
layer.triggerRepaint()
I'm reasonably certain there's not a good way to do this. Here's a terrible, terrible workaround.
Cover the legend text that you want italicized with a label with the same text in italics. Select the legend and the label and Group Items
so you don't accidentally move one without the other.
Ta DA!
Sadly, this isn't a joke. I used to do this a lot. Then I decided to just live with un-italicized botanical names, and hope that my former Plant Systematics professor never sees one of my maps. At least the genus is capitalized and the specific epithet is lower case.
Best Answer
Three years late perhaps. But this can easily be done in QGIS 2.8, which is the current long term release, onwards.
You can set a legend format, "%1" and "%2" are used as place holders for the lower and upper limits of the range. Other characters can be inserted as needed.
There is also a option to limit the precision, so perhaps to 0 in your case.