1. 程式人生 > 實用技巧 >Qt Qgis 二次開發——滑鼠點選識別向量要素

Qt Qgis 二次開發——滑鼠點選識別向量要素

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 二次開發——基本用法