[Qgis-developer] how to add labels to PluginLayer? - please help!
Mikhail Tchernychev
misha at mail.geometrics.com
Mon Oct 14 16:50:49 PDT 2013
It appeared that I made it work. It might be not the best way,but it works.
It also might get outdated as new labeling engine being developed.
1. In the __init__ I add:
self.fields = QgsFields()
self.label = QgsLabel(self.fields)
Not sure I need to keep object "QgsLabel" but I do.
Also self.fields are actually not used, this is just requirement
for "QgsLabel"constructor
2. In the function to draw label:
# again, "fields" has nothing inside
fields = QgsFields()
feat = QgsFeature(fields)
# init number of attributes with the same number of fields
# as enum LabelFields in the QgsLabel class
# this is just for convenience and is not very nice
feat.initAttributes(QgsLabel.LabelFieldCount)
# set feature geometry as a point. Here x,y is position of the label
feat.setGeometry(QgsGeometry.fromPoint(QGpsPoint(x,y)))
# now we have two options to set label options: or we use
# label itself or we use feature "feat" It is better to use label itself
# for font, color etc if we draw bunch of labels. Here is how can be done:
# ( example from my dialog)
self.label.labelAttributes().setFamily(self.dlg.label_attributes.family())
self.label.labelAttributes().setBold(self.dlg.label_attributes.bold())
self.label.labelAttributes().setItalic(self.dlg.label_attributes.italic())
self.label.labelAttributes().setUnderline(self.dlg.label_attributes.underline())
self.label.labelAttributes().setStrikeOut(self.dlg.label_attributes.strikeOut())
self.label.labelAttributes().setSize(self.dlg.label_attributes.size(),
core.QgsLabelAttributes.PointUnits)
self.label.labelAttributes().setColor(QtGui.QColor.fromRgba(self.dlg.label_attributes.color().rgba()))
# the rest of the attribute we set using feature "feat"
# note that above call feat.initAttributes(QgsLabel.LabelFieldCount)is
# essential for to allow setting these attributes as below
feat.setAttribute(QgsLabel.Angle,90.)
feat.setAttribute(QgsLabel.Alignment, 'right')
# etc. Noe that now we need to tell QgsLabel where to search for the
attributes.
# Because we created the same number of feature attributes as size of
enum LabelField
# we can tell it this way:
self.label.setLabelField(QgsLabel.Text,QgsLabel.Text)
self.label.setLabelField(QgsLabel.Angle,QgsLabel.Angle)
self.label.setLabelField(QgsLabel.Alignment,QgsLabel.Alignment)
# Note that first and second argument is the same but this is only
# because number of feature attributes was set the same as LabelField size.
# many of the attributes this way remain unset.
# note that you also can specify font, color etc using
self.label.setLabelField()
# finally draw the labels. I used function drawLabels(self, renderContext)
# Just add:
self.label.renderLabel(renderContext, feat, False)
Best Regards,
Mikhail
On 10/14/2013 12:01 PM, Mikhail Tchernychev wrote:
> Hi,
>
> Apparently no one is to help me... I found it is not simply possible
> to put
> labels at where I want on the plugin layer. Apparently no one is using
> QGsLabel
> anymore, and documentation is missing or obscure...
>
> Qgis keeps crashing on me when I try to add label in python, which I
> believe is a bug
> anyway.
>
> Regards,
> Mikhail
>
> Original post:
>
>> Hi,
>>
>> I am trying to add labels to the plugin layer. What I use in draw()
>> is:
>>
>> fields = QgsFields()
>> fields.append(QgsField('0', QtCore.QVariant.String, 'string', 32))
>> fields.append(QgsField('8', QtCore.QVariant.Double, 'double', 8))
>> fields.append(QgsField('9', QtCore.QVariant.Double, 'double', 8))
>>
>> feat = QgsFeature(fields)
>> label = QgsLabel(fields)
>> feat.setAttribute('0','Test Text')
>> feat.setAttribute('8', -8.66774)
>> feat.setAttribute('9', 37.08391)
>>
>> label.renderLabel(renderContext, feat, False)
>>
>> - QGIS crashes (2.0.1, stock version, Linux)
>>
>> There is almost no doc on how to add labels to the layer
>> I am trying to mimic field 'Text' 'XCoordinate' and 'YCoordinate'
>> with '0', '8' and '9' based on enum LabelField
>>
>> Apparently also some change in the Api since 1.8, I was
>> using GridPlugin from 1.8 as example, but something is different now.
>>
>> Could anyone shed some light on it?
>>
>> I am actually developing Lat/Lon grid plugin for QGIS, which I think
>> very much needed.
>>
>> Thank you
>> Mikhail
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
More information about the Qgis-developer
mailing list