.h部分
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QGraphicsView>
#include <QLabel>
#include <QMouseEvent>
class Widget : public QGraphicsView
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void readMap();
QPointF mapToMap(QPointF);//用于实现场景坐标系与地图之间的映射,以获得某点的经纬度
public slots:
void slotZoom(int);
protected:
//完成地图显示的功能
void DrawWindowBackground(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 // WIDGET_H
.cpp
#include "widget.h"
#include <QSlider>
#include <QGridLayout>
#include <QFile>
#include <QTextStream>
#include <QGraphicsScene>
#include <math.h>
#include <QPixmap>
Widget::Widget(QWidget *parent)
{
//读取地图信息
readMap();
zoom = 50;
int width =map.width();
int height = map.height();
QGraphicsScene* scene = new QGraphicsScene(this);
//限定场景的显示区域为地图的大小
scene->setSceneRect(-width/2,-height/2,width,height);
this->setScene(scene);
setCacheMode(CacheBackground);
//用于地图缩放的滚动条
QSlider* slider = new QSlider;
slider->setOrientation(Qt::Vertical);
slider->setRange(1,100);
slider->setTickInterval(10);
slider->setValue(50);
connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int)));
QLabel* zoominLable = new QLabel;
zoominLable->setScaledContents(true);
zoominLable->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("GraphicsView"));
sceneCoord = new QLabel;
QLabel* label3= new QLabel(tr("map:"));
mapCoord = new QLabel;
//坐标显示区布局
QGridLayout* gridLayout = new QGridLayout;
gridLayout->addWidget(label1,0,0);
gridLayout->addWidget(viewCoord,0,1);
gridLayout->addWidget(label2,1,0);
gridLayout->addWidget(sceneCoord,1,1);
gridLayout->addWidget(label3,2,0);
gridLayout->addWidget(mapCoord,2,1);
gridLayout->setSizeConstraint(QLayout::SetFixedSize);
QFrame* coorFrame = new QFrame;
coorFrame->setLayout(gridLayout);
//缩放布置子布局
QVBoxLayout* zoomLayout = new QVBoxLayout;
zoomLayout->addWidget(zoominLable);
zoomLayout->addWidget(slider);
zoomLayout->addWidget(zoomoutLabel);
//坐标显示区域布局
QVBoxLayout* coordLayout = new QVBoxLayout;
coordLayout->addWidget(slider);
coordLayout->addStretch();
//主布局
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addLayout(zoomLayout);
mainLayout->addLayout(coordLayout);
mainLayout->addStretch();
mainLayout->setMargin(30);
mainLayout->setSpacing(10);
setLayout(mainLayout);
setWindowTitle("Map Widget");
setMinimumSize(600,400);
}
void Widget::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 Widget::slotZoom(int value)
{
qreal s;
if(value>zoom)
{
s = pow(1.01,value-zoom);
}
else
{
s= pow(1/1.01,zoom-value);
}
scale(s,s);
zoom = value;
}
//完成地图显示的功能
void Widget::DrawWindowBackground(QPainter* painter,const QRectF& rect)
{
painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()),map);
}
void Widget::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 Widget::mapToMap(QPointF p)
{
QPointF latLon;
qreal w= sceneRect().width();
qreal h = sceneRect().height();
qreal lon = y1-((h/2+p.y())*abs(y1-y2)/h);
qreal lat = x1+((w/2+p.x())*abs(x1-x2)/w);
latLon.setX(lat);
latLon.setY(lon);
return latLon;
}
Widget::~Widget()
{
}