10.3.2 QGIS线段类型注释(Annotation)的应用与二次开发实现_qgsannotationlineitem-CSDN博客
目录
前言
- 介绍注释(Annotation)图层在QGis中的使用以及二次开发的实现
- 说明:文章中的示例代码均来自开源项目qgis_cpp_api_apps
注释(Annotation)图层
表示包含一组地理参考注释的地图层,例如标记、直线、多边形或文本项。
QGis中的注释(Annotation)图层
- 首先创建一个Annotation图层,如下图所示

- 创建线段类型注释,如下图

二次开发实现线段类型注释(Annotation)
Represents a map layer containing a set of georeferenced annotations, e.g.markers, lines, polygons or text items.
Annotation layers store a set of QgsAnnotationItem items, which are rendered according to the item's z-order.
- 类**
QgsAnnotationLayer
**是注释层,详情见文档,类图如下

- 其成员函数**
QString addItem (QgsAnnotationItem *item)
**用于向图层添加项目 - **
QgsAnnotationItem
**是添加注释项目的基类,详情见文档,类图如下

- **
QgsAnnotationItem
**的几个基类,分别对应QGis中点、线、多边形和文本注释
线段类型注释Item
- 类**
QgsAnnotationLineItem
**是线类型注释,详情见文档,类图如下

- 成员函数void setSymbol( QgsLineSymbol *symbol)
;
可以设置line item的符号 - 构造函数需要传入参数**
QgsCurve *curve
**
cpp
auto lineString = new QgsLineString(points);
//构造并添加
auto lineItem = new QgsAnnotationLineItem(lineString);
- **
QgsCurve
**是曲线类,详情见文档,类图如下

- 这里使用的**
QgsLineString
**类,代表一条线段,关于QGis中几何图形的相关类后边会有详细介绍 - 完整测试代码如下
cpp
void MainWindow::annotationLineSlot()
{
//添加注释图层
QgsAnnotationLayer *annotationLayer = addTestAnnotationLayer();
//定义两个端点
QgsPoint p1(20.33989,-33.86805);
QgsPoint p2(20.47760,-33.86676);
QVector<QgsPoint> points;
points << p1 << p2;
auto lineString = new QgsLineString(points);
//构造并添加
auto lineItem = new QgsAnnotationLineItem(lineString);
annotationLayer->addItem(lineItem);
}
- 效果如下图


总结
- 介绍了线段类型注释(Annotation)的应用与二次开发实现