I'm using QGIS 1.8. I'd like to customize label features on an existing shapefile using the new label engine and it's "data defined settings" option. I'd like to know what the attribute table field types should be for each of the settings: All 7 font options, all 2 buffer options, and all 6 position options. Should they all be numeric fields, or some text, or both? I want to create these fields in the attribute table correctly so that I can map them to the label field settings. I can't find information on the proper field types anywhere. Thanks for your thoughts.

2 Answers 2


There is not a complete set of documentation for the data defined labeling features, but here is a breakdown of their values, and how they are used by QGIS's PAL labeling engine in version 1.8:

  • Size [real] Defines the font size, though how the number is interpreted is dependent upon the setting for the layer (points or map units), which can not be data defined in 1.8.
  • Bold, Italic, Underline, Strikeout [integer] Boolean of 1 or 0 (on/off) for styling the font. It is best to use a typeface that actually contains a bold or italic font for those stylings.
  • Color [string] A named color value as accepted by the QColor::setNamedColor( name ) public function. Regular hexadecimal format of #RRGGBB is recommended.
  • Family [string] A QFont family name for font matching. Note that you can specify foundry name as well. Consult the QFont description (in the docs for your version of Qt) for more info on font matching by name.
  • BufferSize [real] Defines the buffer size in mm. There is not an option to set this in map units in 1.8. The buffer consists of a filled and stroked copy of the label's text, but behind it. The stroke is applied on-the-line, meaning the visual buffering beyond the text will be half of defined size.

    There will be noticeable artifacts in the buffer at large sizes, relative to the text size (Qt issue with the technique used). If your font size for the label is defined in map units, and you set a buffer in mm, there will be visual issues with the two scaling at different rates when zooming on screen, though they can have a consistent look when printing the map to a known scale, for which you will have to figure the correct buffer in mm.
  • BufferColor [string] Same as text color above. Note that in 1.8 this is applied to both the buffer's fill and stroke.
  • Label Distance [real] Defined distance for the label from the feature, as determined by the dynamic PAL layout solution for the given layer type (under the Advanced tab), e.g. above or below line, from point, etc.

    Note: this only seems to work when you have not 'pinned' the label down with data defined X and Y coordinates, i.e. the label's location is still dynamically figured. The distance is relative to the layer-level setting for mm or map units under the Advanced tab. Again your table column will need to be configured for your layer's coordinate type (e.g. lat/long).
  • X Coordinate, Y Coordinate [real] Defined coordinates in the CRS of the layer, e.g. lat/long for some and meters for others. The location these coordinates define is the rotation point of the label, which can change relative to the engine's placement technique or how you have defined horizontal and vertical alignments.

    While QGIS can do on-the-fly coordinate transformations, it is best to have your labeling layers all in the same CRS, so there is no issues with labels shifting due to a funky transformation. Since X and Y column field values in your data source are relative to the CRS, you will need a much higher precision when working with lat/long than with meters, though they will both need a good sized width, relative to your desired mapping precision.

The following columns can not be data defined unless X and Y are already defined. This is a regression from some features of the older labeling engine (like defining rotation independent from X and Y), but is how the advanced labeling currently works in 1.8.

  • Horizontal Alignment [string] (Left, Center, Right) Horizontal alignment of the label's text relative to the label's rotation point.

  • Vertical Alignment [string] (Bottom, Base, Half, and Cap or Top) Vertical alignment of the label's text relative to the label's rotation point. Cap and Top are treated the same. Base is the baseline for the font.

    Note: Changing either horizontal or vertical alignments after defining X and Y will cause the text to shift relative to the label's fixed rotation point, possibly not what you want. The rotation point may be different than the default (Left, Bottom) given the placement method (e.g. point layer with over point set will default to Center, Half, see picture). It is best to experiment with the Change Label tool to 'see' where the rotation point is and how changing the alignment settings will affect the label.

enter image description here
Label with Center and Half alignments and moved to new location, offset from feature point

  • Rotation [real] (0.00-360.00) Number of degrees in counterclockwise rotation of label about rotation point. The Rotate Label tool, when using the Ctl (Cmd on Mac) modifier key will round the rotation increments to 15 degrees.

If you are willing to try out the nightly builds of QGIS from the master branch, or compile it yourself, you will find current work in this area which already includes the following:

enter image description here
Advanced labeling toolbar with 'GIS style' icons

  • Pin/Unpin Labels tool to interactively 'pin or unpin' the label by automatically storing the PAL engine's label positional data to the attribute table's X, Y and optionally Rotation fields. Companion tool to highlight pinned labels.

  • Show/Hide Labels tool to interactively show or hide a label by using a new data defined field called, amazingly enough, 'Show label.'

  • Ongoing enhancements to the label's characteristics, like scale-dependent showing, transparency, and font styling.


You could give a try to new EasyCustomLabeling plugin (http://hub.qgis.org/projects/easycustomlabeling) that duplicates a layer and adds those "data defined" labeling fields.. Here are fields definitions types used in that plugin:

QgsField( "LblX", QVariant.Double, "numeric", 10, 2)
QgsField( "LblY", QVariant.Double, "numeric", 10, 2)
QgsField( "LblAlignH", QVariant.String, "varchar", 12)
QgsField( "LblAlignV", QVariant.String, "varchar", 12)
QgsField( "LblSize", QVariant.Int, "integer", 1 )
QgsField( "LblRot", QVariant.Double, "numeric", 10, 2)
QgsField( "LblBold", QVariant.Int, "integer", 1)
QgsField( "LblItalic", QVariant.Int, "integer", 1)
QgsField( "LblColor", QVariant.String, "varchar", 7)
QgsField( "LblFont", QVariant.String, "varchar", 64)
QgsField( "LblUnder", QVariant.Int, "integer", 1)

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.