QGIS二次开发之选择要素功能

本文探讨了QGIS的二次开发,重点关注选择要素功能。通过研究QgsMapToolSelectionHandler和QgsMapToolIdentify类,作者发现选择图形时会触发geometryChanged信号。重写QgsMapToolIdentifyAction类以实现选中要素的高亮和数据展示。通过监听sigSelectFeatureChange信号,可以获取选中要素的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过查看qgis源码,发现qgis标识的功能在QgsMapToolSelectionHandler类和QgsMapToolIdentify类中;在选择图形时QgsMapToolSelectionHandler发出geometryChanged信号,然后通过重写的QgsMapToolIdentifyAction类进行接收,从而实现选中图形的高亮和以及数据的显示。

参考这两个类和官方文档,重写了一个选择工具类
.h文件

#include <QObject>
#include <QList>
#include <QScopedPointer>
#include<qmath.h>
#include <qgsmapcanvas.h>
#include <qgsmaptoolidentify.h>
#include <qgsrubberband.h>
#include <qgsmapmouseevent.h>
class QgsMapToolSelectFeatures : public QgsMapToolIdentify
{
   
    Q_OBJECT
public:
    QgsMapToolSelectFeatures(QgsMapCanvas *mapCanvas);
protected:
    //重写鼠标移动
    void canvasMoveEvent(QgsMapMouseEvent *e) override;
    //重写鼠标按下
    void canvasPressEvent(QgsMapMouseEvent *e) override;
    //重写鼠标抬起
    void canvasReleaseEvent(QgsMapMouseEvent *e) override;

    void initRubberBand();
    void identifyFromGeometry();
signals:
	//发出选中的要素信号
    void sigSelectFeatureChange(QList<QgsFeature>);
private:
    // 是否正在选择
    bool mSelectionActive = false;
    QScopedPointer<QgsRubberBand> mSelectionRubberBand;
    QColor mFillColor = QColor(
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值