QT&3 仿真停车与红绿灯实现

仿真停车与红绿灯实现

描述,在停车场外设计红绿灯十字路口,实现两辆车A和B,A车路线过红绿灯不进停车场,B车过红绿灯且进入停车场,可循环进行
效果图

红绿灯为四个或者可以成为两对,因为同一条路两边的红绿灯显示效果是一样的,红绿灯的控制函数如下

void MainWindow::wheel1(){
    QImage image6(":/picture/yellow.jpg");
    ui->Yellow_1->setPixmap(QPixmap::fromImage(image6));
    ui->Yellow_4->setPixmap(QPixmap::fromImage(image6));
    ui->Yellow_5->setPixmap(QPixmap::fromImage(image6));
    ui->Yellow->setPixmap(QPixmap::fromImage(image6));
    ui->Yellow_1->hide();
    ui->Yellow->hide();
    ui->Yellow_4->hide();
    ui->Yellow_5->hide();
    QImage image7(":/picture/green.jpg");
    ui->Green_1->setPixmap(QPixmap::fromImage(image7));
    ui->Green->setPixmap(QPixmap::fromImage(image7));
    ui->Green_4->setPixmap(QPixmap::fromImage(image7));
    ui->Green_5->setPixmap(QPixmap::fromImage(image7));
    ui->Green_1->hide();
    ui->Green->hide();
    //ui->Green_4->show();
    //ui->Green_5->show();
    QImage image8(":/picture/red.jpg");
    ui->Red_1->setPixmap(QPixmap::fromImage(image8));
    ui->Red->setPixmap(QPixmap::fromImage(image8));
    ui->Red_4->setPixmap(QPixmap::fromImage(image8));
    ui->Red_5->setPixmap(QPixmap::fromImage(image8));
    ui->Red_4->hide();
    ui->Red_5->hide();
}
void MainWindow::run()
{
   settimer1 = 20;
   settimer2 = -1;
   settimer3 = 3;
   settimer4 = 20;
   settimer5 = -1;
   settimer6 = 3;
   ui->lcdNumber_1->setDigitCount(2);
   ui->lcdNumber_1->setMode(QLCDNumber::Dec);
   ui->lcdNumber_1->setSegmentStyle(QLCDNumber::Filled);
   ui->lcdNumber->setDigitCount(2);
   ui->lcdNumber->setMode(QLCDNumber::Dec);
   ui->lcdNumber->setSegmentStyle(QLCDNumber::Filled);

   ui->lcdNumber_4->setDigitCount(2);
   ui->lcdNumber_4->setMode(QLCDNumber::Dec);
   ui->lcdNumber_4->setSegmentStyle(QLCDNumber::Filled);
   ui->lcdNumber_5->setDigitCount(2);
   ui->lcdNumber_5->setMode(QLCDNumber::Dec);
   ui->lcdNumber_5->setSegmentStyle(QLCDNumber::Filled);

   ui->lcdNumber_1->display(settimer1);
   ui->lcdNumber->display(settimer1);
   ui->lcdNumber_4->display(settimer4);
   ui->lcdNumber_5->display(settimer4);
   timer = new QTimer();
   timer->setInterval(1000);
   timer->start();

   connect(timer,SIGNAL(timeout()),this, SLOT(onTimerOutA()));
   connect(timer,SIGNAL(timeout()),this, SLOT(onTimerOutB()));
   //connect(this,SIGNAL(greenflag),this, SLOT(throughLight()));
   wheel1();

}
void MainWindow::onTimerOutA(){

   cout<<"settimer1="<Car_1->setText(info);
    return info;
}
//切割小车信息
void MainWindow::splitCarInfor(QString carInfo,vector& 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());
     }
}
//获取系统时间
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_2->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_2->x()+ui->Car_2->height() == ui->ScanBtn_2->x())
    {
//        QString carInfo = ui->Car_1->text();
        showToExit(carInfo);
    }

}
//入口显示
void MainWindow::showToEnter(QString carInfo)
{

    vector carSpliteInfo;  //小车的信息划分存储
    splitCarInfor(carInfo,carSpliteInfo);
    vector v;
    v.push_back("车牌:");
    v.push_back("停车费:");
    v.push_back("车型:");
    v.push_back("车位:");

    string s ;
    string sub;
    for(int i=0;iEntrancelabel->setText(s.c_str());

}
//出口显示
void MainWindow::showToExit(QString carInfo)
{

    vector carSpliteInfo;  //小车的信息划分存储
    splitCarInfor(carInfo,carSpliteInfo);
    vector v;
    v.push_back("车牌:");
    v.push_back("停车费:");
    v.push_back("车型:");
    v.push_back("车位:");

    string s ;
    string sub;
    for(int i=0;iExitlabel->setText(s.c_str());

}
//停车时间
void MainWindow::stopCarTime(unsigned int msec)
{
    sleep(msec);
}

补充一点。通过前面可以看出,在小车行进的时候再触发按钮,小车极有可能会“错乱”因此,在控制小车的行动的函数开头加上

ui->CarADriveBtn->setAttribute(Qt::WA_TransparentForMouseEvents, true);
ui->CarBDriveBtn->setAttribute(Qt::WA_TransparentForMouseEvents, true);

进行按钮的“透明化”或者锁定
在行动函数的末尾加上

ui->CarADriveBtn->setAttribute(Qt::WA_TransparentForMouseEvents, false);
ui->CarBDriveBtn->setAttribute(Qt::WA_TransparentForMouseEvents, false);

进行按钮的恢复
最后控制小车行动的函数这里不再赘述,完整程序中可以自行理解。

完整程序

链接:https://pan.baidu.com/s/19HEvFLaj3_S4LY4REnwZ_g
提取码:x4ui

你可能感兴趣的:(QT编程)