Qt Qgis 二次开发——鼠标点击识别矢量要素

2023-05-26,,

Qt Qgis 二次开发——鼠标点击识别矢量要素

介绍:

识别矢量要素需要用到QGis的一个工具类:QgsMapToolIdentifyFeature

一个QgsMapTool的子类的子类,官方文档描述:

接下来就是如何使用了,直接上代码

代码:

使用(不知道基本用法的可以参考上一篇)

#include "qgsmaptoolidentifyfeature.h"

/* 第一个参数:使用该工具的画布
* 第二个参数:要识别的要素的图层
*/
QgsMapToolIdentifyFeature * m_identify = new QgsMapToolIdentifyFeature(m_canvas,m_pLayer); // 创建识别工具 connect(m_identify,SIGNAL(featureIdentified(QgsFeature)),this,SLOT(slo_selectFeature(QgsFeature))); // 关联识别工具识别后的信号,识别后的操作写在槽函数中
// connect(m_identify,SIGNAL(featureIdentified(QgsFeatureId)),this,SLOT(slo_selectFeature(QgsFeatureId));
m_canvas->setMapTool(m_identify); // 设置工具到画布

槽函数操作

void Widget::slo_selectFeature(QgsFeature f)
{
QgsAttributes field = f.attributes(); // 保存要素属性 /* 将要素的属性写到表格中
*/
ui->infoTable->insertRow(ui->infoTable->rowCount());
ui->infoTable->setItem(ui->infoTable->rowCount()-1,0,new QTableWidgetItem(field.at(0).toString()));
ui->infoTable->setItem(ui->infoTable->rowCount()-1,1,new QTableWidgetItem(field.at(1).toString()));
ui->infoTable->setItem(ui->infoTable->rowCount()-1,2,new QTableWidgetItem(field.at(2).toString()));
ui->infoTable->setItem(ui->infoTable->rowCount()-1,3,new QTableWidgetItem(field.at(3).toString()));
}

效果:

上一篇:QT QGIS 二次开发——基本用法

Qt Qgis 二次开发——鼠标点击识别矢量要素的相关教程结束。

《Qt Qgis 二次开发——鼠标点击识别矢量要素.doc》

下载本文的Word格式文档,以方便收藏与打印。