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("\t终点: ")); 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("\t\t绘制最短路径")); 52 53 connect (findPathBtn, SIGNAL(clicked(bool)), this, SLOT(FindPath())); 54 55 clearBtn = new QToolButton; 56 clearBtn->setText (tr("\t\t清除")); 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 }
时间: 2024-10-08 15:48:50