Qt QGraphics类应用——地图缩放选点
功能:
1、地图缩放
2、显示鼠标点经纬度
需求:
1、一张地图
2、地图左上角经纬度和地图右下角经纬度
成品:
代码:
mapwidget.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 point);//用于场景坐标与地图坐标转换 protected: void drawBackground(QPainter *painter, const QRectF &rect); void mouseMoveEvent(QMouseEvent *event);//拖动 void wheelEvent(QWheelEvent *event); //缩放 private: QPixmap map; qreal zoom; QLabel *sceneCoord,*viewCoord,*mapCoord; double x1,x2,y1,y2; int scaleValue; }; #endif // MAPWIDGET_H
mapwidget.cpp
#include "mapwidget.h" #include <QFile> #include <QDebug> #include <qmath.h> #include <QTextStream> #include <QGraphicsScene> #define ZOOM_IN_TIMES -5 #define ZOOM_OUT_TIMES 5 MapWidget::MapWidget() { readMap(); scaleValue = 0; int width = map.width(); int height = map.height(); QGraphicsScene *scene = new QGraphicsScene(this); scene->setSceneRect( -width/2,-height/2,width,height );//中点和范围 setScene(scene); setCacheMode(CacheBackground);//缓存背景 QLabel *scenelabel = new QLabel(tr("GraphicsScene:"),this); QLabel *viewlabel = new QLabel(tr("GraphicsView: "),this); QLabel *maplabel = new QLabel(tr("Map: "),this); sceneCoord = new QLabel(this); viewCoord = new QLabel(this); mapCoord = new QLabel(this); scenelabel->setGeometry(1,1,110,20); viewlabel->setGeometry(1,21,110,20); maplabel->setGeometry(1,41,110,20); sceneCoord->setGeometry(111,1,200,20); viewCoord->setGeometry(111,21,200,20); mapCoord->setGeometry(111,41,200,20); } void MapWidget::readMap() { QString mapName; QFile mapFile(":/file/maps.txt"); int ok = mapFile.open(QIODevice::ReadOnly | QIODevice::Text); qDebug()<<"ok="<<ok; if(ok) { QTextStream in(&mapFile); if(!in.atEnd()) { in>>mapName; in>>x1>>y1>>x2>>y2; } } mapFile.close(); map.load(":/image/China.jpg"); qDebug()<<"mapName="<<mapName<<" x1="<<x1<<" y1="<<y1<<" x2="<<x2<<" y2="<<y2; } QPointF MapWidget::mapToMap(QPointF point)//场景坐标到地图坐标 { QPointF lonlat; qreal w = sceneRect().width(); qreal h = sceneRect().height(); qreal lon = x1+( (w/2+point.x())*qAbs(x1-x2)/w ); qreal lat = y1-( (h/2+point.y())*qAbs(y1-y2)/h ); lonlat.setX(lon); lonlat.setY(lat); return lonlat; } void MapWidget::drawBackground(QPainter *painter, const QRectF &rect) { painter->drawPixmap( (int)sceneRect().left(),(int)sceneRect().top(),map ); } void MapWidget::wheelEvent(QWheelEvent *event) { qreal s=0; bool in = true; bool out = true; if(event->delta() >= 0)//delta返回滑动的距离 { scaleValue += 1; } if(event->delta() < 0)//delta返回滑动的距离 { scaleValue -= 1; } if( scaleValue < ZOOM_IN_TIMES ) { scaleValue = ZOOM_IN_TIMES; in = false; return; }else if( scaleValue > ZOOM_OUT_TIMES ) { scaleValue = ZOOM_OUT_TIMES; out = false; return; } if(in) { s = qPow(1.01,event->delta()/10); } if(out) { s = qPow(1/1.01,-event->delta()/10); } qDebug()<<"scaleValue="<<scaleValue; scale(s,s); } void MapWidget::mouseMoveEvent(QMouseEvent *event) { //1.而由event->globalPos()获取的鼠标位置是鼠标偏离电脑屏幕左上角 //(x=0,y=0)的位置; //2.由pos()获取的位置是主窗口(widget窗口)左上角(边框的左上角,外左上 //角)相对于电脑屏幕的左上角的(x=0,y=0)偏移位置 //视图坐标 QPoint viewPoint = event->pos(); viewCoord->setText( QString::number(viewPoint.x())+","+\ QString::number(viewPoint.y()) ); //场景坐标 QPointF scenePoint = mapToScene(viewPoint); sceneCoord->setText( QString::number(scenePoint.x())+","+\ QString::number(scenePoint.y()) ); //地图坐标 QPointF mapPoint = mapToMap(scenePoint); mapCoord->setText( QString::number(mapPoint.x(),10,7)+","+\ QString::number(mapPoint.y(),10,7) ); }
main.cpp
#include "mapwidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MapWidget map; map.show(); return a.exec(); }
map.txt
:/image/China.jpg 114.4665527 35.96022297 119.9597168 31.3911575