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()));
}
效果: