QT_地图导航

摘要:
代码已更新:http://www.cnblogs.com/douzujun/p/6272667.html1//地图显示功能2#ifndefMAPWIDGET_H3#defineMAPWIDGETH45#include6#include7#include8#include9#include˂QMouse

代码已经更新:http://www.cnblogs.com/douzujun/p/6272667.html

 QT_地图导航第1张

 1 //地图显示功能
 2 #ifndef MAPWIDGET_H
 3 #define MAPWIDGET_H
 4 
 5 #include <QGraphicsView>
 6 #include <QLabel>
 7 #include <QLineEdit>
 8 #include <QPushButton>
 9 #include <QMouseEvent>
10 
11 class MapWidget : public QGraphicsView
12 {
13     Q_OBJECT
14 public:
15 
16     MapWidget();
17     void readMap();                //读取地图信息
18     QPointF mapToMap(QPointF);     //用于实现场景坐标系与地图坐标间的映射.以获得某点的经纬度
19 
20 public slots:
21     void slotZoom(int);            //缩放信号
22 protected:
23     //完成地图的显示功能
24     void drawBackground (QPainter *painter, const QRectF &rect);
25 
26     void mouseMoveEvent (QMouseEvent *event);
27 private:
28     QPixmap map;
29     qreal zoom;
30     QLabel *viewCoord;  //视图目前坐标(以窗口左上为原点)
31     QLabel *sceneCoord; //场景目前坐标(以场景中心为原点)
32     QLabel *mapCoord;   //地图目前坐标
33 
34     double x1, y1;
35     double x2, y2;
36 
37 };
38 
39 #endif // MAPWIDGET_H
//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对象为主窗口连接一个场景
    QGraphicsScene *scene = new QGraphicsScene(this);
    //限定场景的显示区域为地图大小
    scene->setSceneRect (-width/2, -height/2, width, height);
    setScene (scene);
    /***
     * The background is cached(隐藏,缓存). This affects both custom backgrounds, and
     * backgrounds based on the backgroundBrush property. When this flag is enabled,
     * QGraphicsView will allocate one pixmap with the full size of the viewport
     */
    setCacheMode (CacheBackground);
    /***
     * 用于地图缩放的滑动条
     * 新建一个QSlider对象作为地图的缩放控制
     */
    QSlider *slider = new QSlider;
    //设置滚动条方向-垂直
    slider->setOrientation (Qt::Vertical);
    slider->setRange (1, 100);        //设置地图缩放比例值范围为0~100
    slider->setTickInterval (10);     //显示刻度间隔为10
    slider->setValue (0);            //设置当前初始值为50
    // 将缩放控制条的valueChanged()信号与地图缩放slotZoom()槽函数相关联
    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, 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);         //设置对话框(或窗体)的边距为30
    mainLayout->setSpacing (10);        //设定各个控件之间的间距为10
    setLayout (mainLayout);             //在场景中设置为布局
    setWindowTitle ("Map Widget");
    setMinimumSize (600, 400);
}

//读取地图信息
void MapWidget::readMap ()
{
    QString mapName;
    //新建一个QFile对象,“map.txt"是描述地图信息的文本文件
    QFile mapFile("maps.txt");
    //以"只读"的方式打开此文件
    int ok = mapFile.open (QIODevice::ReadOnly);
    if (ok)                   //分别以读取地图的名称和四个经纬度信息
    {
        QTextStream ts(&mapFile);
        if (!ts.atEnd ()) {              //没有到末尾返回false
            ts >> mapName;               //储存字符串
            ts >> x1 >> y1 >> x2 >> y2;  //储存地图左上角和右下角的经纬度
        }
    }
    map.load (mapName);      //将地图读取至私有标量map中
}

//根据缩放滑动条的当前值,确定缩放的比例,调用scale()函数实现地图缩放--完成地图缩放功能slotZoom
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;            //当前放大值
}

//drawBackground()--以地图图片重绘场景的背景来实现地图显示
void MapWidget::drawBackground (QPainter *painter, const QRectF &rect)
{
    /***
     * The scene rectangle defines the extent of the scene, and in the view's case,
     * this means the area of the scene that you can navigate using the scroll bars.
     */
    painter->drawPixmap (int(sceneRect ().left ()) + 10, int(sceneRect ().top ()) - 10, map);

//    //Demo 在图片上绘线
//    QPen pen;
//    pen.setWidth (4);
//    pen.setColor (Qt::red);
//    painter->setPen (pen);
//    painter->setBrush (Qt::red);
//    painter->drawLine (100, 10, 100, 100);

}

//完成某点在各层坐标中的映射及显示
void MapWidget::mouseMoveEvent (QMouseEvent *event)
{
    //QGraphicesView 坐标
    QPoint viewPoint = event->pos ();   //鼠标事件位置
    viewCoord->setText (QString::number (viewPoint.x ()) + "," + QString::number (viewPoint.y ()));

    //QGraphiccsScene 坐标 -- 将视图坐标转换成场景坐标
    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 ()));


}

//完成从场景至地图坐标的转换mapToMap()
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;
}
 1 //mainwidget
 2 #ifndef MAINWINDOW_H
 3 #define MAINWINDOW_H
 4 
 5 #include <QMainWindow>
 6 #include "mapwidget.h"
 7 #include <QToolButton>
 8 #include <QLabel>
 9 #include <QComboBox>
10 #include <QSpinBox>
11 #include <QTextEdit>
12 
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16 
17 public:
18     MainWindow(QWidget *parent = 0);
19     ~MainWindow();
20     void createToolBar();
21     void paintEvent (QPaintEvent *);
22 public slots:
23     void setStartStation();
24     void setEndStation();
25     void FindPath();
26     void Clear();
27 private:
28     MapWidget *mapWidget;
29     QLabel *startLabel;
30     QLabel *endLabel;
31     QComboBox *startComboBox;
32     QComboBox *endComboBox;
33     QToolButton *findPathBtn;
34     QToolButton *clearBtn;
35 };
36 
37 #endif // MAINWINDOW_H
 1 //布局实现-mainwindow.cpp
 2 
 3 #include "mainwindow.h"
 4 #include <QToolBar>
 5 
 6 MainWindow::MainWindow(QWidget *parent)
 7     : QMainWindow(parent)
 8 {
 9     mapWidget = new MapWidget;
10     createToolBar ();  //实现一个工具栏
11     setCentralWidget (mapWidget);
12     setMinimumSize (600, 400);  //设置最小尺寸
13 }
14 
15 MainWindow::~MainWindow()
16 {
17 }
18 
19 void MainWindow::createToolBar ()
20 {
21     QToolBar *toolBar = addToolBar ("Tool");
22     startLabel = new QLabel(tr("起点: "));
23     startComboBox = new QComboBox;
24     startComboBox->addItem (tr("公寓6号楼")); startComboBox->addItem (tr("公寓10号楼"));
25     startComboBox->addItem (tr("公寓5号楼")); startComboBox->addItem (tr("公寓9号楼"));
26     startComboBox->addItem (tr("公寓4号楼")); startComboBox->addItem (tr("公寓8号楼"));
27     startComboBox->addItem (tr("公寓3号楼")); startComboBox->addItem (tr("公寓7号楼"));
28     startComboBox->addItem (tr("公寓2号楼"));
29     startComboBox->addItem (tr("公寓1号楼")); startComboBox->addItem (tr("图书馆"));
30     startComboBox->addItem (tr("一食堂")); startComboBox->addItem (tr("西操场"));
31     startComboBox->addItem (tr("公寓23号楼")); startComboBox->addItem (tr("公寓13号楼"));
32     startComboBox->addItem (tr("公寓22号楼")); startComboBox->addItem (tr("公寓12号楼"));
33 
34     endLabel = new QLabel(tr("	终点: "));
35 
36     endComboBox = new QComboBox;
37     endComboBox->addItem (tr("公寓6号楼")); endComboBox->addItem (tr("公寓10号楼"));
38     endComboBox->addItem (tr("公寓5号楼")); endComboBox->addItem (tr("公寓9号楼"));
39     endComboBox->addItem (tr("公寓4号楼")); endComboBox->addItem (tr("公寓8号楼"));
40     endComboBox->addItem (tr("公寓3号楼")); endComboBox->addItem (tr("公寓7号楼"));
41     endComboBox->addItem (tr("公寓2号楼"));
42     endComboBox->addItem (tr("公寓1号楼")); endComboBox->addItem (tr("图书馆"));
43     endComboBox->addItem (tr("一食堂"));    endComboBox->addItem (tr("西操场"));
44     endComboBox->addItem (tr("公寓23号楼"));endComboBox->addItem (tr("公寓13号楼"));
45     endComboBox->addItem (tr("公寓22号楼"));endComboBox->addItem (tr("公寓12号楼"));
46 
47     connect (startComboBox, SIGNAL(activated(int)), this, SLOT(setStartStation()));
48     connect (endComboBox, SIGNAL(activated(int)), this, SLOT(setEndStation()));
49 
50     findPathBtn = new QToolButton;
51     findPathBtn->setText (tr("		绘制最短路径"));
52 
53     connect (findPathBtn, SIGNAL(clicked(bool)), this, SLOT(FindPath()));
54 
55     clearBtn = new QToolButton;
56     clearBtn->setText (tr("		清除"));
57 
58     connect (clearBtn, SIGNAL(clicked(bool)), this, SLOT(Clear()));
59 
60     toolBar->addWidget (startLabel);
61     toolBar->addWidget (startComboBox);
62     toolBar->addWidget (endLabel);
63     toolBar->addWidget (endComboBox);
64     toolBar->addWidget (findPathBtn);
65     toolBar->addWidget (clearBtn);
66 }
67 
68 void MainWindow::setStartStation ()
69 {
70 
71 }
72 
73 void MainWindow::setEndStation ()
74 {
75 
76 }
77 
78 void MainWindow::FindPath ()
79 {
80 
81 }
82 
83 void MainWindow::Clear ()
84 {
85 
86 }
87 
88 void MainWindow::paintEvent (QPaintEvent *)
89 {
90 
91 }

免责声明:文章转载自《QT_地图导航》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合JEECMS站群管理系统-- 标签使用和模板的制作下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

GCC选项之-M

大多数的C/C++编译器都支持一个“-M”的选项,即自动找寻源文件中包含的头文件。举个例子,比如mian.c包含有如下头文件。 #include <stdio.h> #include "log.h"        用gcc -M main.c就可以输入其包含的所有头文件。如下图所示。  [root@bogon CodeNotes]# gc...

QMesageBox的使用

一、使用构造函数弹出对话框 1、 QMessageBox msgBox;//最简单的对话框,里面什么也没有   QString str = “test”;   msgBox.setText(str);   msgBox.exec(); 2、   QMessageBox message(QMessageBox::NoIcon, "Title", "Conte...

Linux GTK编程示例

1 #include <gtk/gtk.h> 2 3 static void helloWorld(GtkWidget *wid, GtkWidget *win) 4 { 5 GtkWidget *dialog = NULL; 6 dialog = gtk_message_dialog_new(GTK_WINDOW...

QThread与QWidget使用

本文主要内容: 在任务一中,用 四 种方式实现:点击界面按钮,开线程运行一段程序,结果显示在一个Label上。1. 用不正确的方式得到看似正确的结果2. 用Qt Manual 和 例子中使用的方法3. 用一种好用但被Qt开发人员批判的方法4. 用一种被开发人员强烈推荐,但Qt Manual和例子中只字未提的方法 为了简单起见,本文只讲如何做及其结果是什么...

C/C++ #include&amp;lt;&amp;gt; 和 #include""的区别

我们都听过这样的说法: 使用#include<> 程序会直接到标准函数库中找文件 使用#include"" 程序会会先从当前目录中找文件,如果找不到会再到标准函数库中找文件 所以,一般情况下我们引用标准函数库中的头文件时,用#include<>能有更高的效率,但是其实它的查找顺序非常严格: 1.#include<> 1....

gdal 在qt中的使用

gdal是著名的开源GIS数据操作库,包括arcgis在内的软件都在用它。 官网地址如下:http://www.gdal.org/  支持的格式很多。包括 基础的图片png,gif,jpg,tiff,bmp pci,erdas,arcinfo的遥感影像数据 shp,tab,mif等的矢量文件 oracle spatial,postsql的空间数据库 打算结...