• widget.h

     #ifndef MAPWIDGET_H
    #define MAPWIDGET_H #include <QGraphicsView>
    #include <QLabel>
    #include <QMouseEvent> class MapWidget : public QGraphicsView
    {
    Q_OBJECT
    public:
    MapWidget(); void readMap(); //读取地图信息
    QPointF mapToMap(QPointF); //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值 public slots:
    void slotZoom(int); protected:
    void drawBackground(QPainter *painter, const QRectF &rect); //完成地图显示的功能
    void mouseMoveEvent(QMouseEvent *event); //鼠标移动功能 private:
    QPixmap map; //地图
    qreal zoom; //大小
    QLabel *viewCoord; //标签
    QLabel *sceneCoord;
    QLabel *mapCoord;
    double x1,y1;
    double x2,y2;
    }; #endif // MAPWIDGET_H
  • widget.cpp
     #include "widget.h"
    #include <QSlider>
    #include <QGridLayout>
    #include <QFile>
    #include <QTextStream>
    #include <QGraphicsScene>
    #include <math.h> MapWidget::MapWidget()
    {
    readMap(); //读取地图信息 zoom=; int width = map.width();
    int height = map.height();
    QGraphicsScene *scene = new QGraphicsScene(this);
    scene->setSceneRect(-width/,-height/,width,height);
    setScene(scene);
    setCacheMode(CacheBackground); //用于地图缩放的滑动条
    QSlider *slider = new QSlider;
    slider->setOrientation(Qt::Vertical);
    slider->setRange(,);
    slider->setTickInterval();
    slider->setValue();
    connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int))); QLabel *zoominLabel = new QLabel;
    zoominLabel->setScaledContents(true);
    zoominLabel->setPixmap(QPixmap("zoomin.png")); QLabel *zoomoutLabel = new QLabel;
    zoomoutLabel->setScaledContents(true);
    zoomoutLabel->setPixmap(QPixmap("zoomout.png")); //坐标值显示区
    QLabel *label1 = new QLabel(tr("GraphicsView:"));
    viewCoord = new QLabel;
    QLabel *label2 = new QLabel(tr("GraphicsScene:"));
    sceneCoord = new QLabel;
    QLabel *label3 = new QLabel(tr("map:"));
    mapCoord = new QLabel; //坐标显示区布局
    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(label1,,);
    gridLayout->addWidget(viewCoord,,);
    gridLayout->addWidget(label2,,);
    gridLayout->addWidget(sceneCoord,,);
    gridLayout->addWidget(label3,,);
    gridLayout->addWidget(mapCoord,,);
    gridLayout->setSizeConstraint(QLayout::SetFixedSize); QFrame *coordFrame = new QFrame;
    coordFrame->setLayout(gridLayout); //缩放控制子布局
    QVBoxLayout *zoomLayout = new QVBoxLayout;
    zoomLayout->addWidget(zoominLabel);
    zoomLayout->addWidget(slider);
    zoomLayout->addWidget(zoomoutLabel); //坐标显示区域布局
    QVBoxLayout *coordLayout = new QVBoxLayout;
    coordLayout->addWidget(coordFrame);
    coordLayout->addStretch(); //主布局
    QHBoxLayout *mainLayout = new QHBoxLayout;
    //滑动
    mainLayout->addLayout(zoomLayout);
    //坐标
    mainLayout->addLayout(coordLayout);
    mainLayout->addStretch();
    mainLayout->setMargin();
    mainLayout->setSpacing();
    setLayout(mainLayout); setWindowTitle("Map Widget");
    setMinimumSize(,);
    } void MapWidget::readMap() //读取地图信息
    {
    QString mapName;
    QFile mapFile("maps.txt");
    //打开文件
    int ok = mapFile.open(QIODevice::ReadOnly);
    if(ok)
    {
    QTextStream ts(&mapFile);
    if(!ts.atEnd())
    {
    ts>>mapName;
    ts>>x1>>y1>>x2>>y2;
    }
    }
    //载入地图
    map.load(mapName);
    } void MapWidget::slotZoom(int value) //地图缩放与放大
    {
    qreal s;
    if(value>zoom) //放大
    {
    s=pow(1.01,(value-zoom));
    }
    else //缩小
    {
    s=pow(/1.01,(zoom-value));
    }
    scale(s,s);//放大缩小
    // rotate(s);
    zoom = value;
    } //绘制背景
    void MapWidget::drawBackground(QPainter *painter, const QRectF &rect)
    {
    painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()), map);//绘图
    } void MapWidget::mouseMoveEvent(QMouseEvent *event)
    {
    //QGraphicsView 坐标
    QPoint viewPoint = event->pos();
    viewCoord->setText(QString::number(viewPoint.x())+","+QString::number(viewPoint.y())); //QGraphicsScene 坐标
    QPointF scenePoint = mapToScene(viewPoint);
    sceneCoord->setText(QString::number(scenePoint.x())+","+QString::number(scenePoint.y())); //地图坐标(经、纬度值)
    QPointF latLon = mapToMap(scenePoint);
    mapCoord->setText(QString::number(latLon.x())+","+QString::number(latLon.y()));
    } //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值
    QPointF MapWidget::mapToMap(QPointF p)
    {
    QPointF latLon;
    qreal w =sceneRect().width();
    qreal h =sceneRect().height(); qreal lon = y1-((h/+p.y())*abs(y1-y2)/h);
    qreal lat = x1+((w/+p.x())*abs(x1-x2)/w); latLon.setX(lat);
    latLon.setY(lon); return latLon;
    }
  • main.c
     #include "widget.h"
    #include <QApplication> int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    //设置字体
    QFont font("ARPL KaitiM GB",);
    font.setBold(true);
    a.setFont(font); //显示
    MapWidget w;
    w.show(); return a.exec();
    }

36.QT地图的更多相关文章

  1. Foundations of Qt Development 学习笔记 Part1 Tips1-50

    1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...

  2. html代码转义到js时,往往会遇到问题,这代码实现html和js互转

    这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  3. Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)

    摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...

  4. Qt5.7学习

    一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...

  5. Qt 学习之路 2(36):二进制文件读写

    Qt 学习之路 2(36):二进制文件读写 豆子 2013年1月6日 Qt 学习之路 2 20条评论 在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice ...

  6. Qt开发北斗定位系统融合百度地图API及Qt程序打包发布

    Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...

  7. Qt的QWebChannel和JS、HTML通信/交互驱动百度地图

    Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...

  8. Qt加载百度离线地图

    1.下载百度地图离线API 1.3 下载链接:http://download.csdn.NET/detail/caoshangpa/9476608,网上虽然出现了2.0版本离线API,但是经试用,存在 ...

  9. Qt编写安防视频监控系统17-在线地图

    一.前言 在线地图模块在一开始设计整个系统的时候就考虑进去了,主要功能就是在摄像机管理中,提供经纬度信息,然后加载百度地图在浏览器中显示,根据摄像机信息表中的每个摄像机的经纬度信息,自动生成设备点在地 ...

随机推荐

  1. WinForm上传文件,下载文件

    上传文件: 使用OpenFileDialog控件选择文件, 具体代码示例: private void btnUpLoadPic_Click(object sender, EventArgs e) { ...

  2. 深度讲解智能硬件手机APP开发流程

    常州做APP开发公司紫竹云科技分析,智能硬件产品的软件开发,除了APP和后台之外还有一个固件端的开发,由于固件是要运行产品上的,不过此时的硬件也是刚开始进行研发,所以是无法提供硬件来运行固件的.因此在 ...

  3. 【数据分析学习】Pandas思维导图

    点我查看原版

  4. nginx (待更新)

    install apt install nginx 默认在 /etc/nginx 目录下 一个 master 以及多个 worker 3504 root 20 0 141M 7196 5552 S 0 ...

  5. springboot 打包下载数据

    //文件打包下载     public static HttpServletResponse downLoadFiles(List<File> files,             Htt ...

  6. javascript事件列表解说

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  7. Hibernate 的核心配置文件

    核心配置文件 <!-- SessionFactory,相当于之前学习连接池配置 --> <session-factory> <!-- 1 基本4项 --> < ...

  8. MySQL数据库唯一性设置(unique index)

    1,命令行操作 分为两种.一种是在建表时就想好要加上唯一性,另一种是在后期才发现需要设置唯一性. 建表时: CREATE TABLE `t_user` ( `Id` int(11) NOT NULL ...

  9. ThinkPHP3.1.3分表状态时候的自动验证的代码BUG

    问题描述 ThinkPHP3.1.3 当使用TP的分库分表后 有些地方需要使用Model自动验证create,当验证唯一性unique会出现BUG, 具体描述 因为自动验证检测唯一性会使用隐式的使用f ...

  10. 1.2 为Eclipse绑定Tomcat

    1.window→preferences打开属性窗口 2.点击add 3.点击Tomcat6.0 点击next 4.选择tomcat的解压目录和jdk,并点击finish 5.点击ok 6.打开ser ...