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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM