在本项目中简单仿真真实停车场的,例如信息扫描,栅栏开关,并且此次采用相对位置坐标控制移动
效果图
核心代码
//设置小车文本信息
QString MainWindow::setCarInfo(QString carName,QString money,QString brand,QString park )
{
QString info = carName+" "+money+" "+brand+" "+park+" ";
//ui->Car_1->setText(info);
return info;
}
//将小车信息写入文件
//void MainWindow::writeTofile(QString carInfo)
//{
// QFile f("D:\\QtWork\\CarParking\\text.txt");
// if(!f.open(QIODevice::WriteOnly|QIODevice::Append))
// {
// qDebug()<<"Open failed.";
// }else
// {
// QTextStream txtOutput(&f);
// txtOutput<<carInfo<<endl;
// }
// f.close();
//}
//切割小车信息
void MainWindow::splitCarInfor(QString carInfo,vector<string>& v)
{
string str = carInfo.toStdString();
while(1)
{
int idx = str.find(" ");
if(idx==-1)
{
break;
}
string s = str.substr(0,idx);
v.push_back(s);
str = str.substr(idx+1,str.length());
}
}
//延时函数,控制小车速度
void MainWindow::sleep(unsigned int msec){
QTime newTime = QTime::currentTime().addMSecs(msec);
while(QTime::currentTime() < newTime)
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
//获取系统时间
string MainWindow::getSysTime()
{
QDateTime local(QDateTime::currentDateTime());
mLocalTime = local.toString().toStdString();
return mLocalTime;
}
//入口扫描车的信息
void MainWindow::scanCar()
{
QString carInfo = setCarInfo("A-00000","10RMB","BWM","P3");
if(ui->Car_1->x() == ui->ScanBtn_1->x())
{
// QString carInfo = ui->Car_1->text();
showToEnter(carInfo);
}
}
//出口扫描仪扫描获取小车信息
void MainWindow::scanCarExit()
{
QString carInfo = setCarInfo("A-00000","10RMB","BWM","P3");
if(ui->Car_1->x()+ui->Car_1->height() == ui->ScanBtn_2->x())
{
// QString carInfo = ui->Car_1->text();
showToExit(carInfo);
}
}
//入口扫描车的信息
void MainWindow::scanCar1()
{
QString carInfo = setCarInfo("A-00000","10RMB","BWM","P8");
if(ui->Car_1->x() == ui->ScanBtn_1->x())
{
// QString carInfo = ui->Car_1->text();
showToEnter(carInfo);
}
}
//出口扫描仪扫描获取小车信息
void MainWindow::scanCarExit1()
{
QString carInfo = setCarInfo("A-00000","10RMB","BWM","P8");
if(ui->Car_1->x()+ui->Car_1->height() == ui->ScanBtn_2->x())
{
// QString carInfo = ui->Car_1->text();
showToExit(carInfo);
}
}
//入口显示
void MainWindow::showToEnter(QString carInfo)
{
vector<string> carSpliteInfo; //小车的信息划分存储
splitCarInfor(carInfo,carSpliteInfo);
vector<string> v;
v.push_back("车牌:");
v.push_back("停车费:");
v.push_back("车型:");
v.push_back("车位:");
string s ;
string sub;
for(int i=0;i<carSpliteInfo.size();++i)
{
cout<<carSpliteInfo[i]<<" ";
sub = v[i]+carSpliteInfo[i]+'\n';
s += sub;
}
// carSpliteInfo.push_back(mLocalTime);
s += getSysTime();
ui->Entrancelabel->setText(s.c_str());
}
//出口显示
void MainWindow::showToExit(QString carInfo)
{
vector<string> carSpliteInfo; //小车的信息划分存储
splitCarInfor(carInfo,carSpliteInfo);
vector<string> v;
v.push_back("车牌:");
v.push_back("停车费:");
v.push_back("车型:");
v.push_back("车位:");
string s ;
string sub;
for(int i=0;i<carSpliteInfo.size();++i)
{
cout<<carSpliteInfo[i]<<" ";
sub = v[i]+carSpliteInfo[i]+'\n';
s += sub;
}
s += getSysTime();
ui->Exitlabel->setText(s.c_str());
}
//靠近停车场AB
void MainWindow::DriveClose(){
//向下
for(int i=ui->Car_1->y();i<490;++i){
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(10);
}
trans90();
}
//进入并停车A
void MainWindow::Drivein(){
int car_x = ui->Car_1->x();
int car_y = ui->Car_1->y();
FencesPowerRu(car_x,car_y);
for(int i=ui->Car_1->x(); i+ui->Car_1->height()> ui->P3line->x()-INSTANCE; --i)
{
ui->Car_1->setGeometry(i,ui->Car_1->y(),
ui->Car_1->width(),
ui->Car_1->height()
);
scanCar();
RedEnterCheckRu(i,ui->Car_1->y());
sleep(15);
}
ui->Entrancelabel->setText("欢迎~");
trans90();
for(int i=ui->Car_1->y();i+ui->Car_1->height()< ui->Pdown->y()-INSTANCE;++i)
{
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(15);
}
}
// 进入并停车B
void MainWindow::Drivein1(){
int car_x = ui->Car_1->x();
int car_y = ui->Car_1->y();
FencesPowerRu(car_x,car_y);
for(int i=ui->Car_1->x(); i+ui->Car_1->height()> ui->P3line->x()-INSTANCE; --i)
{
ui->Car_1->setGeometry(i,ui->Car_1->y(),
ui->Car_1->width(),
ui->Car_1->height()
);
scanCar1();
RedEnterCheckRu(i,ui->Car_1->y());
sleep(15);
}
ui->Entrancelabel->setText("欢迎~");
trans270();
for(int i=ui->Car_1->y();i-ui->Car_1->height()> ui->Pup->y()-3*INSTANCE;--i)
{
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(15);
}
}
//离开停车位A
void MainWindow::Driveback(){
//向上
for(int i=ui->Car_1->y();i>490;--i){
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(20);
}
trans270();
}
//离开停车位B
void MainWindow::Driveback1(){
//向上
for(int i=ui->Car_1->y();i<490;++i){
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(20);
}
trans90();
}
//离开停车场A
void MainWindow::Driveout(){
int car_x = ui->Car_1->x();
int car_y = ui->Car_1->y();
FencesPowerChu(car_x,car_y);
for(int i=ui->Car_1->x(); i+ui->Car_1->width()> ui->Roadleft_2->x()+INSTANCE; --i)
{
ui->Car_1->setGeometry(i,ui->Car_1->y(),
ui->Car_1->width(),
ui->Car_1->height()
);
scanCarExit();
RedEnterCheckChu(i,ui->Car_1->y());
sleep(15);
}
ui->Exitlabel->setText("再见~");
trans90();
for(int i=ui->Car_1->y();i+ui->Car_1->width()> ui->Roadup_1->y()+7*INSTANCE;--i)
{
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(10);
}
trans90();
//向右
for(int i=ui->Car_1->x(); i+ui->Car_1->width()<ui->Roadright_1->x()-INSTANCE; ++i){
ui->Car_1->setGeometry(i,ui->Car_1->y(),
ui->Car_1->width(),
ui->Car_1->height());
sleep(10);
}
trans90();
}
//离开停车场B
void MainWindow::Driveout1(){
int car_x = ui->Car_1->x();
int car_y = ui->Car_1->y();
FencesPowerChu(car_x,car_y);
for(int i=ui->Car_1->x(); i+ui->Car_1->width()> ui->Roadleft_2->x()+INSTANCE; --i)
{
ui->Car_1->setGeometry(i,ui->Car_1->y(),
ui->Car_1->width(),
ui->Car_1->height()
);
scanCarExit1();
RedEnterCheckChu(i,ui->Car_1->y());
sleep(15);
}
ui->Exitlabel->setText("再见~");
trans90();
for(int i=ui->Car_1->y();i+ui->Car_1->width()> ui->Roadup_1->y()+7*INSTANCE;--i)
{
ui->Car_1->setGeometry(ui->Car_1->x(),i,
ui->Car_1->width(),
ui->Car_1->height());
sleep(10);
}
trans90();
//向右
for(int i=ui->Car_1->x(); i+ui->Car_1->width()<ui->Roadright_1->x()-INSTANCE; ++i){
ui->Car_1->setGeometry(i,ui->Car_1->y(),
ui->Car_1->width(),
ui->Car_1->height());
sleep(10);
}
trans90();
}
//停车时间
void MainWindow::stopCarTime(unsigned int msec)
{
sleep(msec);
}
//顺时针90°转向函数
void MainWindow::trans90()
{
ui->Car_1->setGeometry(ui->Car_1->x(),ui->Car_1->y(),ui->Car_1->height(),ui->Car_1->width());//将lable的height与width调换。使得转向后图片可以完整显示
QPixmap pixmap(":/picture/xia.png");
matrix.rotate(90.0);//旋转角度
ui->Car_1->setPixmap(pixmap.transformed(matrix,Qt::SmoothTransformation)); //图片顺时针旋转90度
}
//逆时针90°转向函数
void MainWindow::trans270()
{
ui->Car_1->setGeometry(ui->Car_1->x(),ui->Car_1->y(),ui->Car_1->height(),ui->Car_1->width());//将lable的height与width调换。使得转向后图片可以完整显示
QPixmap pixmap(":/picture/xia.png");
matrix.rotate(270.0);//旋转角度
ui->Car_1->setPixmap(pixmap.transformed(matrix,Qt::SmoothTransformation)); //图片逆时针旋转90度
}
//红外检测入口栅栏打开
void MainWindow::FencesEnterOn()
{
int distanceInEnterDoor = ui->Roadentance_2->y()-ui->Roadentance_1->y();
ui->fencesEnter->setGeometry(ui->fencesEnter->x(),
ui->fencesEnter->y(),
ui->fencesEnter->width(),
ui->fencesEnter->height() - distanceInEnterDoor);
}
//红外检测入口栅栏关闭
void MainWindow::FencesEnterOff()
{
int distanceInEnterDoor = ui->Roadentance_2->y()-ui->Roadentance_1->y();
ui->fencesEnter->setGeometry(ui->fencesEnter->x(),
ui->fencesEnter->y(),
ui->fencesEnter->width(),
ui->fencesEnter->height()+distanceInEnterDoor);
}
//检测入口栅栏何时打开
void MainWindow::FencesPowerRu(int car_x,int car_y)
{
int fences_x = ui->fencesEnter->x();
int car_h = ui->Car_1->height();
for(int i=car_x; i-car_h>fences_x+CHECK_LENGTH; --i)
{
ui->Car_1->setGeometry(i,car_y,
ui->Car_1->width(),
ui->Car_1->height());
sleep(20);
}
FencesEnterOn();
}
//红外检测入口栅栏何时关闭
void MainWindow::RedEnterCheckRu(int car_x,int car_y)
{
if(car_x + ui->Car_1->height() == ui->enterRed->x())
FencesEnterOff();
}
//红外检测出口栅栏打开
void MainWindow::FencesExitOn()
{
int distanceInExitDoor = ui->Roadexit_2->y()-ui->Roadexit_1->y();
ui->fencesExit->setGeometry(ui->fencesExit->x(),
ui->fencesExit->y(),
ui->fencesExit->width(),
ui->fencesExit->height()-distanceInExitDoor);
}
//红外检测出口栅栏关闭
void MainWindow::FencesExitOff()
{
int distanceInExitDoor = ui->Roadexit_2->y()-ui->Roadexit_1->y();
ui->fencesExit->setGeometry(ui->fencesExit->x(),
ui->fencesExit->y(),
ui->fencesExit->width(),
ui->fencesExit->height()+distanceInExitDoor);
}
//检测出口栅栏何时打开
void MainWindow::FencesPowerChu(int car_x,int car_y)
{
int fences_x = ui->fencesExit->x();
int car_h = ui->Car_1->height();
for(int i=car_x; i-car_h>fences_x+CHECK_LENGTH; --i)
{
ui->Car_1->setGeometry(i,car_y,
ui->Car_1->width(),
ui->Car_1->height());
sleep(20);
}
FencesExitOn();
}
//红外检测出口栅栏何时关闭
void MainWindow::RedEnterCheckChu(int car_x,int car_y)
{
if(car_x + ui->Car_1->height() == ui->exitRed->x())
FencesExitOff();
}
//“驶进停车场A”按钮点击事件,开始小车驶入
void MainWindow::on_inButton_clicked()
{
DriveClose();
Drivein();
//DriveClose1();
}
//“驶出停车场A”按钮点击事件,开始小车驶出
void MainWindow::on_outButton_clicked()
{
Driveback();
Driveout();
}
//“Close”按钮点击事件,关闭窗口
void MainWindow::on_pushButton_2_clicked()
{
close();
}
//“驶进停车场B”按钮点击事件,开始小车驶入
void MainWindow::on_inButton_2_clicked()
{
DriveClose();
Drivein1();
}
//“驶出停车场B”按钮点击事件,开始小车驶出
void MainWindow::on_outButton_2_clicked()
{
Driveback1();
Driveout1();
}
完整代码
链接:https://pan.baidu.com/s/13_tC7R-23loSl1BSVP8mZQ
提取码:suwf