框架实现地图浏览"/>
使用QTGraphics View 框架实现地图浏览
该程序基本功能:地图的浏览、放大、缩小以及显示各点的坐标
1、头文件
mainwindow.h#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = 0);~MainWindow();
};#endif // MAINWINDOW_H
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);//用于实时场景坐标系与地图坐标之间的映射,以获得某点的经纬度值
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
源文件
main.cpp#include "mainwindow.h"
#include <QApplication>
#include"mapwidget.h"
#include<QFont>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QFont font("ARPL KaitiM GB",12);font.setBold(true);a.setFont(font);MapWidget mapWidget;mapWidget.show();// MainWindow w;//w.show();return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{
}MainWindow::~MainWindow()
{}
mapwidget.cpp #include "mapwidget.h"
#include<QSlider>
#include<QGridLayout>
#include<QFile>
#include<QTextStream>
#include<QGraphicsScene>
#include<math.h>
MapWidget::MapWidget()
{readMap();//读取地图信息zoom=50;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);//用于地图缩放的滑动条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* zoominLabel=new QLabel;zoominLabel->setScaledContents(true);zoominLabel->setPixmap(QPixmap("F://image//zoomin.jpg"));QLabel* zoomoutLabel=new QLabel;zoomoutLabel->setScaledContents(true);zoomoutLabel->setPixmap(QPixmap("F://image//zoomout.jpg "));//坐标值显示区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,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* 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(30);mainLayout->setSpacing(10);setLayout(mainLayout);setWindowTitle("Map Widget");setMinimumSize(600,400);}
void MapWidget::readMap()
{QString mapName;// QFile mapFile("F://qt project//maps.txt");QFile mapFile("F://qt project//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/1.01,(zoom-value));}scale(s,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/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;
}
运行结果:
注意:需要新建一个maps.txt文件,里边内容包括地图的路径,以及四个点的经纬度
更多推荐
使用QTGraphics View 框架实现地图浏览
发布评论