QT_校园导航Update

时间:2020-12-24 23:43:21

QT_校园导航Update

QT_校园导航Update

QT_校园导航Update

 //MainWidget.h

 #ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include "mapwidget.h"
#include <QToolButton>
#include <QGraphicsLineItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QTextEdit>
#include <QPainter>
#include <QVector> class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = );
~MainWindow();
void createToolBar();
void paintEvent (QPaintEvent *);
void setStart(int X, int Y);
void setEnd(int X, int Y);
void setNextPos (int index);
void initScene();
public slots:
void setStartStation();
void setEndStation();
void FindPath();
void Clear();
private:
MapWidget *mapWidget;
QLabel *startLabel;
QLabel *endLabel;
QComboBox *startComboBox;
QComboBox *endComboBox;
QToolButton *findPathBtn;
QToolButton *clearBtn; QGraphicsScene *scene;
QGraphicsView *view; int startX, startY, endX, endY;
QVector<int> nextPath; struct ArcCell{ //弧信息
int adj; //对无权图有1,0表示是否相邻,对带权图,则为权值类型
// string info; //该弧的相关信息
}; //内部类
static const int MAX_VERTEX_NUM = ;
static const int INF = ; struct MGraph{
QVector<int> vexs; //顶点集合
//临接矩阵
ArcCell arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum; //顶点数
int arcnum; //边数
// int kind; //图的类型
}; class DijkstraFindPath
{
public:
DijkstraFindPath();
MGraph mgraph;
void CreateGraph();
// void ShortestPath(int v0, PathMatrix &path, ShortPathTable &DP, int prev[]);
int prev[MAX_VERTEX_NUM]; //最短路上的前驱顶点
int d[MAX_VERTEX_NUM]; //表示边e = (u,v)的权值(不存在时为INF,不过d[i][i]=0)
bool used[MAX_VERTEX_NUM]; //已经使用过的图
void dijkstra(int startPos); //求从起点startPos出发到各个顶点的最短距离
QVector<int> get_Path(int endPos);//到顶点endPos的最短路
}; DijkstraFindPath *dj;
}; #endif // MAINWINDOW_H
 //MainWidget.cpp
//最短路径算法,和界面的实现 #include "mainwindow.h"
#include <qdebug.h>
#include <QToolBar>
#include <QtAlgorithms>
#include <iostream> MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// mapWidget = new MapWidget;
// painter = new QPainter();
dj = new MainWindow::DijkstraFindPath();
dj->CreateGraph (); scene = new QGraphicsScene;
scene->setSceneRect (-, -, , );
initScene(); view = new QGraphicsView;
view->setScene (scene);
view->setMinimumSize (, );
view->show ();
setCentralWidget (view); createToolBar (); //实现一个工具栏
// setCentralWidget (mapWidget);
// setMinimumSize (600, 400); //设置最小尺寸
} MainWindow::DijkstraFindPath::DijkstraFindPath()
{
mgraph.vexnum = ; //初始化点数目
for (int i = ; i < mgraph.vexnum; i++) //初始化点编号
mgraph.vexs.push_back (i);
mgraph.arcnum = ; //暂定
for (int i = ; i < mgraph.vexnum; i++) {
for (int j = ; j < mgraph.vexnum; j++) {
if (i == j)
mgraph.arcs[i][j].adj = ;
else
mgraph.arcs[i][j].adj = INF;
// mgraph.arcs[i][j].info = "";
}
}
} void MainWindow::DijkstraFindPath::CreateGraph ()
{
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //6 - 5
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //6 - 10
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //5 - 4
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //4 - 3
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //3 - 2
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //3 - 22
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //2 - 1
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//23 - 22
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //6 - 23
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //23 - 4
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //1 - 一食堂
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//一食堂-操场
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//一食堂-祁通1
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通1-祁通2(路口)
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //一食堂-岔路口
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //岔路-7
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//一食堂到岔路(通向7号楼的)
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//岔路-祁通大道
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通大道-图书馆
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通大道-祁通2
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通2-方肇周
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //西大门-祁通1
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通1-祁通2
// mgraph.arcs[25][12].adj = mgraph.arcs[12][25].adj = 108; //西大门-西操
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //1 - 操场
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //10 - 9
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //9 - 8
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //8 - 7
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //7 - 图书馆
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //10 - 13
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//13 - 12
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//13 - 16
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//12 - 15
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//16 - 15
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//15 - 14
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//16 - 19
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //19 - 18
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //18 - 17
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//14 - 17
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //17 - 二超
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //二超 - 二食堂
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//二食堂-图书馆 //以下处理细节 mgraph.arcnum = ;
} void MainWindow::DijkstraFindPath::dijkstra (int startPos)
{
for (int i = ; i < mgraph.vexnum; i++) d[i] = INF;
for (int i = ; i < mgraph.vexnum; i++) used[i] = false;
for (int i = ; i < mgraph.vexnum; i++) prev[i] = -;
d[startPos] = ; while (true) {
int v = -;
for (int u = ; u < mgraph.vexnum; u++) {
if (!used[u] && (v == - || d[u] < d[v])) v = u;
} if (v == -) break;
used[v] = true; for (int u = ; u < mgraph.vexnum; u++) {
if (d[u] > d[v] + mgraph.arcs[v][u].adj) {
d[u] = d[v] + mgraph.arcs[v][u].adj;
prev[u] = v;
}
}
}
} QVector<int> MainWindow::DijkstraFindPath::get_Path (int endPos)
{
QVector<int> path; for ( ; endPos != -; endPos = prev[endPos]) {
// std::cout << "EndPos: " << endPos << ", ";
path.push_back (endPos);
} std::reverse(path.begin (), path.end ()); return path;
} void MainWindow::initScene ()
{
QGraphicsPixmapItem *item =
scene->addPixmap (QPixmap("NanTong.jpg"));
// item->setFlag (QGraphicsItem::ItemIsMovable);
item->setPos (-, -);
} MainWindow::~MainWindow()
{ } void MainWindow::createToolBar ()
{
QToolBar *toolBar = addToolBar ("Tool");
startLabel = new QLabel(tr("起点: "));
startComboBox = new QComboBox;
startComboBox->addItem (tr("公寓6号楼")); //
startComboBox->addItem (tr("公寓5号楼")); //
startComboBox->addItem (tr("公寓4号楼")); //
startComboBox->addItem (tr("公寓3号楼")); //
startComboBox->addItem (tr("公寓2号楼")); //
startComboBox->addItem (tr("公寓1号楼")); // startComboBox->addItem (tr("公寓10号楼")); //
startComboBox->addItem (tr("公寓9号楼")); //
startComboBox->addItem (tr("公寓8号楼")); //
startComboBox->addItem (tr("公寓7号楼")); //
startComboBox->addItem (tr("图书馆")); //
startComboBox->addItem (tr("一食堂")); startComboBox->addItem (tr("西操场")); //11 12
startComboBox->addItem (tr("公寓23号楼")); startComboBox->addItem (tr("公寓13号楼")); //13 14
startComboBox->addItem (tr("公寓22号楼")); startComboBox->addItem (tr("公寓12号楼")); //15 16
startComboBox->addItem (tr("公寓楼16")); startComboBox->addItem (tr("公寓楼15")); //17 18
startComboBox->addItem (tr("公寓楼14")); startComboBox->addItem (tr("公寓楼19")); //19 20
startComboBox->addItem (tr("公寓楼18")); startComboBox->addItem (tr("公寓楼17")); //21 22
startComboBox->addItem (tr("二超")); startComboBox->addItem (tr("二食堂")); //23 24
startComboBox->addItem (tr("西大门")); startComboBox->addItem (tr("方肇周教学楼")); //25 26 endLabel = new QLabel(tr("\t终点: ")); endComboBox = new QComboBox;
endComboBox->addItem (tr("公寓6号楼"));
endComboBox->addItem (tr("公寓5号楼"));
endComboBox->addItem (tr("公寓4号楼"));
endComboBox->addItem (tr("公寓3号楼"));
endComboBox->addItem (tr("公寓2号楼"));
endComboBox->addItem (tr("公寓1号楼"));
endComboBox->addItem (tr("公寓10号楼"));
endComboBox->addItem (tr("公寓9号楼"));
endComboBox->addItem (tr("公寓8号楼"));
endComboBox->addItem (tr("公寓7号楼"));
endComboBox->addItem (tr("图书馆"));
endComboBox->addItem (tr("一食堂")); endComboBox->addItem (tr("西操场"));
endComboBox->addItem (tr("公寓23号楼"));endComboBox->addItem (tr("公寓13号楼"));
endComboBox->addItem (tr("公寓22号楼"));endComboBox->addItem (tr("公寓12号楼"));
endComboBox->addItem (tr("公寓楼16")); endComboBox->addItem (tr("公寓楼15"));
endComboBox->addItem (tr("公寓楼14")); endComboBox->addItem (tr("公寓楼19"));
endComboBox->addItem (tr("公寓楼18")); endComboBox->addItem (tr("公寓楼17"));
endComboBox->addItem (tr("二超")); endComboBox->addItem (tr("二食堂"));
endComboBox->addItem (tr("西大门")); endComboBox->addItem (tr("方肇周教学楼")); connect (startComboBox, SIGNAL(activated(int)), this, SLOT(setStartStation()));
connect (endComboBox, SIGNAL(activated(int)), this, SLOT(setEndStation())); findPathBtn = new QToolButton;
findPathBtn->setText (tr("\t\t绘制最短路径")); connect (findPathBtn, SIGNAL(clicked(bool)), this, SLOT(FindPath())); clearBtn = new QToolButton;
clearBtn->setText (tr("\t\t清除")); connect (clearBtn, SIGNAL(clicked(bool)), this, SLOT(Clear())); toolBar->addWidget (startLabel);
toolBar->addWidget (startComboBox);
toolBar->addWidget (endLabel);
toolBar->addWidget (endComboBox);
toolBar->addWidget (findPathBtn);
toolBar->addWidget (clearBtn);
} void MainWindow::setStart(int X, int Y) {
startX = X; startY = Y;
// qDebug() << X << ", " << Y;
} void MainWindow::setEnd (int X, int Y)
{
endX = X; endY = Y;
} void MainWindow::setStartStation ()
{
switch (startComboBox->currentIndex ()) {
case :
setStart(-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart(-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
default:
break;
}
} void MainWindow::setEndStation ()
{
switch (endComboBox->currentIndex ()) {
case :
setEnd(-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
default:
break;
}
} void MainWindow::setNextPos (int index)
{
switch (index) {
case :
setEnd(-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
default:
break;
}
} void MainWindow::FindPath ()
{
//Demo 在图片上绘线 在原有基础上 (+700, +440);
QVector<QPoint> v; dj->dijkstra (startComboBox->currentIndex ());
//设置下一处的终点
nextPath = dj->get_Path (endComboBox->currentIndex ()); //准备绘制
Clear ();
//将路线绘制下来
QGraphicsPathItem *item = new QGraphicsPathItem(); QPen pen;
pen.setWidth ();
pen.setColor (Qt::red);
item->setPen (pen);
item->setFlag (QGraphicsItem::ItemIsPanel); // qDebug() << startX << " " << startY << " " << endX << " " << endY; // qDebug() << "Hello World !"; //设置起点
// v << QPoint(startX + 700, startY + 440); for (int i = ; i < nextPath.size (); i++) {
qDebug() << nextPath[i] << " , ";
}
scene->addItem (item); QPainterPath pa; //path
// setNextPos (nextPath[1]);
// item->setLine (startX + 700, startY + 440, endX + 700, endY + 440);
pa.moveTo (startX + , startY + ); for (int i = ; i < nextPath.size() ; i++) {
setNextPos (nextPath[i]);
pa.lineTo (endX + , endY + );
}
item->setPath (pa); } void MainWindow::Clear ()
{
QList<QGraphicsItem*> listItem = scene->items ();
while (!listItem.empty ())
{
scene->removeItem (listItem.at ());
listItem.removeAt ();
}
QGraphicsPixmapItem *item =
scene->addPixmap (QPixmap("NanTong.jpg"));
// item->setFlag (QGraphicsItem::ItemIsMovable);
item->setPos (-, -);
} void MainWindow::paintEvent (QPaintEvent *)
{ }
 //Main.cpp
#include "mainwindow.h"
#include "mapwidget.h"
#include <QApplication>
#include <QFont>
#include <QDebug> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont font("ARPL KaitiM GB", );
font.setBold (true);
a.setFont (font); qDebug() << "Run............";
MainWindow w;
w.show (); return a.exec();
}