前言
- 介绍标注在QGis中的使用以及二次开发的实现
- 说明:文章中的示例代码均来自开源项目qgis_cpp_api_apps
标注(Labels)
标注是与图层关联的,一般通过图层的属性信息生成,并与矢量要素一一关联。
- protected_areas.shp为例展示标注的使用,默认效果如下
QGis中的标注(Label)
- 打开图层属性,切换到“Labels”选项卡,如下图所示
- 在最上方的下拉列表中选择“Single labels”,各选项的说明如下:
- No labels:无标注。
- Single labels:简单标注。
- Rule-based labeling:基于规则标注。
- Blocking:遮挡,用于调整其他图层的标注布局,防止标注被遮挡。将图层设置为其他图层标签的遮挡时,其本身无须呈现任何标签。
- Value选项中选择需要标注的字段,此处选择"name"
- 通过文字(Text)、格式化(Formatting)、缓冲区(Buffer)、遮罩(Mask)、背景(Background)、阴影(Shadow)、位置(Placement)、渲染(Rendering)选项卡设置格式,如下图
- 属性设置完成后,图层显示效果如下
二次开发实现标注(Label)
QgsVectorLayer
中成员函数setLabeling(QgsAbstractVectorLayerLabeling *labeling)
用于设置标注,函数setLabelsEnabled(true)
开启标注,QgsAbstractVectorLayerLabeling
类图如下
-QgsVectorLayerSimpleLabeling
是QGis中Single Lables,详情见文档
QgsVectorLayerSimpleLabeling
构造函数需要传入类QgsPalLayerSettings
,该类包含了文字(Text)、格式化(Formatting)、缓冲区(Buffer)、遮罩(Mask)、背景(Background)、阴影(Shadow)、位置(Placement)的设置,详情见文档QgsPalLayerSettings
的成员函数setCallout,setPlacementSettings
对应QGis Single Labels中Callouts和PlacementQgsPalLayerSettings
的成员函数setFormat(const QgsTextFormat &format)
用于设置Label的格式,其中参数QgsTextFormat
代表文本格式,详情见文档QgsTextFormat
的函数setBackground(in const QgsTextBackgroundSettings &backgroundSettings)
对应文本背景设置QgsTextFormat
的函数setBuffer(in const QgsTextBufferSettings &bufferSettings)
对应Buffer设置QgsTextFormat
的函数setShadow(in const QgsTextShadowSettings &shadowSettings)
对应于Shadow设置QgsTextFormat
的函数setMask(in const QgsTextMaskSettings &maskSettings)
对应于Mask设置- 以上几个类的关联图如下
- 完整测试代码如下
void MainWindow::labelSlot()
{
//添加一个线图层
QgsVectorLayer* layer = addTestShape(QStringLiteral("maps/shapefile/protected_areas.shp"));
//
//QgsTextFormat --> QgsTextBackgroundSettings
// --> QgsTextBufferSettings
// --> QgsTextMaskSettings
// --> QgsTextShadowSettings
//QgsCallout
//QgsLabelPlacementSettings
//QgsPalLayerSettings包含文本设置
QgsPalLayerSettings settings;
settings.drawLabels = true;
settings.fieldName = "name";
//文字设置
QgsTextFormat textFormat;
QgsTextBackgroundSettings backgroundSettings;
QgsTextBufferSettings bufferSettings;
QgsTextMaskSettings maskSettings;
QgsTextShadowSettings shadowSettings;
//开启背景并设置为白色
backgroundSettings.setEnabled(true);
backgroundSettings.setFillColor(QColor("white"));
textFormat.setBackground(backgroundSettings);
//开启buffer并设置颜色为黑色
bufferSettings.setEnabled(true);
bufferSettings.setColor(QColor("black"));
textFormat.setBuffer(bufferSettings);
//开启mask并设置size
maskSettings.setEnabled(true);
maskSettings.setSize(1.0);
textFormat.setMask(maskSettings);
//开启shadow
shadowSettings.setEnabled(true);
shadowSettings.setColor(QColor("red"));
textFormat.setShadow(shadowSettings);
settings.setFormat(textFormat);
//设置callout
auto callout = new QgsBalloonCallout();
settings.setCallout(callout);
//设置位置
QgsLabelPlacementSettings placementSettings;
placementSettings.setAllowDegradedPlacement(true);
settings.setPlacementSettings(placementSettings);
QgsVectorLayerSimpleLabeling* simpleLabeling = new QgsVectorLayerSimpleLabeling(settings);
layer->setLabelsEnabled(true);
layer->setLabeling(simpleLabeling);
}
- 效果如下图
总结
- 介绍了QGis中Label的使用以及二次开发代码的实现