多线程开始

This commit is contained in:
jackfiled 2022-06-26 13:57:45 +08:00
parent 4709b1ad7c
commit 48cd806197
6 changed files with 175 additions and 11 deletions

View File

@ -13,6 +13,7 @@
#include "string" #include "string"
#include "sstream" #include "sstream"
#include "QObject" #include "QObject"
#include "QDebug"
/** /**
* QObject * QObject
@ -77,6 +78,24 @@ public slots:
*/ */
void ReadConfigFileSlot(const QString& file_name); void ReadConfigFileSlot(const QString& file_name);
/**
*
* @param query_type
* @param node_id id
*/
void AddQuerySlot(int query_type, int node_id) const;
signals:
/**
*
* @param node_num
*/
void RailsCreated(int node_num);
/**
*
*/
void QueryChangedSignal();
private: private:

View File

@ -29,6 +29,17 @@ void BusControllerModel::ReadConfigFileSlot(const QString& file_name)
ReadConfigFile(file_name.toStdString()); ReadConfigFile(file_name.toStdString());
} }
void BusControllerModel::AddQuerySlot(int query_type, int node_id) const
{
rail_node_t *node_pos = rail_manager->FindNode(node_id);
query_manager->CreateQuery(query_type, node_pos);
}
/*
* auto_pilot_bus
* C写成
*/
int BusControllerModel::GetBusPosition() const int BusControllerModel::GetBusPosition() const
{ {
int result = 0; int result = 0;
@ -293,11 +304,12 @@ void BusControllerModel::ReadConfigFile(const std::string& file_name)
chosen_strategy = BUS_FCFS; chosen_strategy = BUS_FCFS;
} }
qDebug() << node_space_length << total_station;
// 得到轨道的总长度 // 得到轨道的总长度
total_distance = node_space_length * total_station; total_distance = node_space_length * total_station;
// 重新生成轨道模型 rail_manager->CreateRails(node_space_length, total_station);
delete rail_manager;
rail_manager = new RailsModel(node_space_length, total_station); emit RailsCreated(total_station);
} }
int BusControllerModel::FCFSDirection() const int BusControllerModel::FCFSDirection() const

View File

@ -6,16 +6,19 @@
#include "header/moc_centralwidget.cpp" #include "header/moc_centralwidget.cpp"
#include "form/ui_CentralWidget.h" #include "form/ui_CentralWidget.h"
#include "centralwidget.h"
CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CentralWidget) CentralWidget::CentralWidget(QWidget *parent, BusControllerModel *bus_controller) : QWidget(parent), ui(new Ui::CentralWidget)
{ {
ui->setupUi(this); ui->setupUi(this);
scene_manager = new SceneManager(20); scene_manager = new SceneManager(20);
ui->main_canva->setScene(scene_manager->scene); ui->main_canva->setScene(scene_manager->scene);
controller = bus_controller;
SetControlConnection();
SetWidgetConnection();
SetupQueryList(); SetupQueryList();
} }
@ -27,6 +30,17 @@ CentralWidget::~CentralWidget()
delete ui; delete ui;
} }
void CentralWidget::SetControlConnection()
{
}
void CentralWidget::SetWidgetConnection()
{
QObject::connect(ui->create_query_button, &QPushButton::clicked,
this, &CentralWidget::AddQueryButtonClicked);
}
void CentralWidget::SetupQueryList() void CentralWidget::SetupQueryList()
{ {
// 设置请求列表的表头 // 设置请求列表的表头
@ -78,3 +92,8 @@ void CentralWidget::DeleteQueryItem(int query_id)
delete *itor; delete *itor;
query_items.erase(itor); query_items.erase(itor);
} }
void CentralWidget::AddQueryButtonClicked()
{
}

View File

@ -12,6 +12,7 @@
#include "mainScene.h" #include "mainScene.h"
#include "queryListItem.h" #include "queryListItem.h"
#include "busModel.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui namespace Ui
@ -25,7 +26,7 @@ class CentralWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
explicit CentralWidget(QWidget *parent = nullptr); explicit CentralWidget(QWidget *parent = nullptr, BusControllerModel *bus_controller = nullptr);
~CentralWidget() override; ~CentralWidget() override;
@ -37,8 +38,17 @@ public slots:
*/ */
void AppendQueryItem(int query_type, int node_id); void AppendQueryItem(int query_type, int node_id);
/**
*
* @param query_id
*/
void DeleteQueryItem(int query_id); void DeleteQueryItem(int query_id);
/**
*
*/
void AddQueryButtonClicked();
private: private:
/** /**
* UI控件 * UI控件
@ -50,11 +60,26 @@ private:
*/ */
SceneManager *scene_manager; SceneManager *scene_manager;
/**
*
*/
BusControllerModel *controller;
/** /**
* *
*/ */
std::list<QueryListItem *> query_items; std::list<QueryListItem *> query_items;
/**
*
*/
void SetControlConnection();
/**
*
*/
void SetWidgetConnection();
/** /**
* *
* *

View File

@ -7,6 +7,7 @@
#include <QMainWindow> #include <QMainWindow>
#include "QFileDialog" #include "QFileDialog"
#include "QThread"
#include "centralwidget.h" #include "centralwidget.h"
#include "busModel.h" #include "busModel.h"
@ -35,17 +36,63 @@ signals:
*/ */
void OpenConfigFileSignal(QString file_name); void OpenConfigFileSignal(QString file_name);
/**
*
*/
void RunBusSignal();
/**
*
*/
void PauseBusSignal();
/**
*
*/
void StopBusSignal();
public slots: public slots:
/** /**
* *
*/ */
void ReadConfigFileButtonClicked(); void ReadConfigFileButtonClicked();
/**
*
*/
void RunBusClicked();
/**
*
*/
void PauseBusClicked();
/**
*
*/
void StopBusClicked();
private: private:
/**
* UI控件
*/
Ui::MainWindow *ui; Ui::MainWindow *ui;
/**
*
*/
CentralWidget *central_widget; CentralWidget *central_widget;
/**
*
*/
BusControllerModel *controller; BusControllerModel *controller;
QThread *worker_thread;
/**
*
*/
void SetMenuBarConnection(); void SetMenuBarConnection();
void SetControlConnection();
}; };
#endif //AUTO_BUS_GUI_MAIN_WINDOW_H #endif //AUTO_BUS_GUI_MAIN_WINDOW_H

View File

@ -6,32 +6,59 @@
#include "header/moc_mainwindow.cpp" #include "header/moc_mainwindow.cpp"
#include "form/ui_MainWindow.h" #include "form/ui_MainWindow.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{ {
ui = new Ui::MainWindow; ui = new Ui::MainWindow;
central_widget = new CentralWidget;
controller = new BusControllerModel; controller = new BusControllerModel;
worker_thread = new QThread;
// 开始多线程
controller->moveToThread(worker_thread);
worker_thread->start();
central_widget = new CentralWidget(nullptr, controller);
ui->setupUi(this); ui->setupUi(this);
this->setCentralWidget(central_widget); this->setCentralWidget(central_widget);
SetMenuBarConnection(); SetMenuBarConnection();
SetControlConnection();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
worker_thread->quit();
delete ui; delete ui;
delete central_widget; delete central_widget;
delete controller; delete controller;
delete worker_thread;
} }
void MainWindow::SetMenuBarConnection() void MainWindow::SetMenuBarConnection()
{ {
QObject::connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close())); // 连接退出按钮
QObject::connect(ui->actionRead_ConfigFile, &QAction::triggered, this, &MainWindow::ReadConfigFileButtonClicked); QObject::connect(ui->actionExit, SIGNAL(triggered()),
this, SLOT(close()));
// 连接读取配置文件操作
QObject::connect(ui->actionRead_ConfigFile, &QAction::triggered,
this, &MainWindow::ReadConfigFileButtonClicked);
// 连接运行公交车按钮
QObject::connect(ui->actionRun_Bus, &QAction::triggered,
this, &MainWindow::RunBusClicked);
// 连接暂停公交车按钮
QObject::connect(ui->actionPause_Bus, &QAction::triggered,
this, &MainWindow::PauseBusClicked);
// 连接停止公交车按钮
QObject::connect(ui->actionStop_Bus, &QAction::triggered,
this, &MainWindow::StopBusClicked);
}
void MainWindow::SetControlConnection()
{
QObject::connect(this, &MainWindow::OpenConfigFileSignal,
controller, &BusControllerModel::ReadConfigFileSlot);
} }
void MainWindow::ReadConfigFileButtonClicked() void MainWindow::ReadConfigFileButtonClicked()
@ -53,3 +80,18 @@ void MainWindow::ReadConfigFileButtonClicked()
emit OpenConfigFileSignal(file_name); emit OpenConfigFileSignal(file_name);
} }
} }
void MainWindow::RunBusClicked()
{
emit RunBusSignal();
}
void MainWindow::PauseBusClicked()
{
emit PauseBusSignal();
}
void MainWindow::StopBusClicked()
{
emit StopBusSignal();
}