【QT + OsgEarth】(六)-- 添加事件控制器获取鼠标坐标

效果图

在左上角显示当前鼠标所在点的经纬度和高度
在这里插入图片描述

实现原理

  • 在视图中添加控件用于显示文本
  • 添加事件控制器捕获鼠标坐标

添加控件

//Label
osgEarth::Util::Controls::LabelControl* mouseCoords;
// 添加视点信息控件
void addViewePointLabel();
    mRoot->addChild(osgEarth::Util::Controls::ControlCanvas::get(mViewer));
    osgEarth::Util::Controls::ControlCanvas* canvas = osgEarth::Util::Controls::ControlCanvas::get(mViewer);
    //添加控件显示视点信息
    //添加控件用于显示鼠标交点信息
    mouseCoords =
            new osgEarth::Util::Controls::LabelControl("test",osg::Vec4f(1.0,1.0,1.0,1.0),18.0f);
    //位置
    mouseCoords->setHorizAlign(osgEarth::Util::Controls::Control::ALIGN_RIGHT);
    mouseCoords->setVertAlign(osgEarth::Util::Controls::Control::ALIGN_BOTTOM);
    mouseCoords->setBackColor(0, 0, 0, 0.5);//背景颜色

    canvas->addControl(mouseCoords);
    if(labelHandler == 0 )
    {
        labelHandler = new LableControlEventHandler(viewCoords, mouseCoords, mapNode);
    }
    mViewer->addEventHandler(labelHandler);

添加事件控制器

新建类 LableControlEventHandler 继承osgGA::GUIEventHandler

class LableControlEventHandler :  public osgGA::GUIEventHandler
{
public:
    LableControlEventHandler(osgEarth::Util::Controls::LabelControl* mouseLabel
                             ,osgEarth::Util::Controls::LabelControl* viewLabel
                             ,osgEarth::MapNode* mapN);

    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override;

    void setMouseLabel(const GeoPoint& coords, osg::View* view, MapNode* mapNode);

private:
    //labels
    osgEarth::Util::Controls::LabelControl* mouseCoords;
    osgEarth::Util::Controls::LabelControl* viewCoords;

    osgEarth::MapNode* mapNode;

    osg::NodePath nodePath;
};

#endif // LABLECONTROLEVENTHANDLER_H

构造函数初始化参数

LableControlEventHandler::LableControlEventHandler(osgEarth::Util::Controls::LabelControl* mouseLabel
                                                   ,osgEarth::Util::Controls::LabelControl* viewLabel
                                                   ,osgEarth::MapNode* mapN)
{
    mouseCoords = mouseLabel;
    viewCoords = viewLabel;
    mapNode = mapN;
    nodePath.push_back((osg::Node *)mapNode->getTerrainEngine());
}

重写函数 handle(const GUIEventAdapter &ea, GUIActionAdapter &aa)

bool LableControlEventHandler::handle(const GUIEventAdapter &ea, GUIActionAdapter &aa)
{
    osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
    if(viewer)
    {
        if(ea.getEventType() == ea.MOVE || ea.getEventType() == ea.DRAG)
        {
            osg::Vec3d world;
            //qDebug()<< " fvv";
            if (mapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), world))
            {
                GeoPoint map;
                map.fromWorld(mapNode->getMapSRS(), world);
                setMouseLabel(map, aa.asView(), mapNode);
            }

        }

        }
    }
    return false;
}

void LableControlEventHandler::setMouseLabel(const GeoPoint &coords, osg::View *view, MapNode *mapNode)
{
    char wsrc[512];
    sprintf(wsrc, " lat:%.3f Lan:%.3f Height:%.3f",coords.x(), coords.y(),coords.z());
    mouseCoords->setText(wsrc);
}
  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
安装QtOSGEarth可以按照以下步骤进行操作: 1. 首先,在Qt官网(https://www.qt.io/)上下载适用于您的操作系统的Qt安装包。根据您的需求选择相应的版本和组件进行安装。 2. 安装Qt时,您可以选择使用在线安装程序或离线安装包。如果有网络连接,建议选择在线安装程序,以便自动下载和安装所需的文件和组件。 3. 在安装过程中,选择合适的安装路径,并确保勾选安装所需的Qt模块。根据您项目的需求,可能需要额外安装Qt Creator等组件。 4. 完成Qt的安装后,您可以打开Qt Creator,创建新的项目,并选择合适的项目模板。 5. 接下来,下载OSGEarth的源代码。您可以在OSGEarth的GitHub页面(https://github.com/gwaldron/osgearth)上找到最新的源代码。 6. 在终端或命令行中,切换到OSGEarth源代码的目录,并执行编译和安装命令。具体命令可能因不同平台和编译选项而有所不同。 7. 编译完成后,您可以将OSGEarth集成到您的Qt项目中。在Qt Creator中,打开您的项目文件(通常是一个.pro文件),并添加OSGEarth的相关头文件和库文件。 8. 根据OSGEarth提供的示例代码和文档,编写您需要的功能和界面。 9. 最后,您可以构建和运行您的Qt项目,测试OSGEarth是否按预期工作。 以上是关于安装QtOSGEarth的一般步骤,具体操作可能因不同的操作系统和版本而有所差异。建议在安装和配置过程中参考各自的官方文档和论坛,以获得更准确和详细的信息。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值