36.QT地图
- 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地图的更多相关文章
- Foundations of Qt Development 学习笔记 Part1 Tips1-50
1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...
- html代码转义到js时,往往会遇到问题,这代码实现html和js互转
这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)
摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...
- Qt5.7学习
一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...
- Qt 学习之路 2(36):二进制文件读写
Qt 学习之路 2(36):二进制文件读写 豆子 2013年1月6日 Qt 学习之路 2 20条评论 在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice ...
- Qt开发北斗定位系统融合百度地图API及Qt程序打包发布
Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...
- Qt的QWebChannel和JS、HTML通信/交互驱动百度地图
Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...
- Qt加载百度离线地图
1.下载百度地图离线API 1.3 下载链接:http://download.csdn.NET/detail/caoshangpa/9476608,网上虽然出现了2.0版本离线API,但是经试用,存在 ...
- Qt编写安防视频监控系统17-在线地图
一.前言 在线地图模块在一开始设计整个系统的时候就考虑进去了,主要功能就是在摄像机管理中,提供经纬度信息,然后加载百度地图在浏览器中显示,根据摄像机信息表中的每个摄像机的经纬度信息,自动生成设备点在地 ...
随机推荐
- mysql连接出现error node【1045】
第一步:在my.ini下找到mysqlid.在后边添加skip-grant-tables 第二步:重新启动mysql服务 第三步:重新设置密码 第四步: 将skip-grant-tables删除掉,保 ...
- Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()
在Android的开发中,凡是需要画图的地方大都离不开类Drawable.Android的官方文档中介绍这个类就是被设计用来表示可以被画的东西.A Drawable is a ge ...
- hdu3873 Invade the Mars 有限制的最短路
此段略过.看完题目,觉得这真的是一道好题目.自己有想法,但是实现起来却很难.看题解,写代码,然后写题解,意义何在?我不认为自己总是这么弱.就算抄代码,我也要有自己的理解.菜鸟总会成长. 首先,题目必须 ...
- 【Oracle】RAC集群中的命令
数据库名称:racdb 节点名称:rac3.rac4 注:以下命令均在grid用户中执行 1.查看集群节点的状态: [grid@rac3 ~]$ crsctl check cluster [grid@ ...
- 高级I/O函数
给套接口上的I/O设置超时 1.调用alarm,在调用超过指定时间时产生SIGALARM信号,这涉及到信号处理,而且可能和进程中其他的alarm冲突 2.使用select阻塞在等待I/O上,selec ...
- 01--vim常用快捷键
Linux中vim编辑器的功能非常强大,许多常用快捷键用起来非常方便,这里将我学vim入门时学的一些常用的快捷键分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉 ...
- Fear No More歌词
"Fear No More" Every anxious thought that steals my breath It's a heavy weight upon my ...
- .net 导入Excel
今天我在做导入Excel的时候遇到了一些问题,顺便说句其实我很少做这方面的!我的需求是导入EXCEL 验证数据正确性 并把数据显示到页面 如有错误信息则弹出来 那具体问题是什么呢? 导入Excel有2 ...
- Python 之 格式化文件
# 结构化文件存储- xml, json- 为了解决不同设备之间的信息交换 ## XML文件(可扩展标记语言) - 标记语言:语言中使用尖括号括起来的文本字符串标记 - 可扩展:用户可以自己定义需要的 ...
- C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用
这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用.HttpModule:Http模块,可以在页面处理前后.应用 ...