程序示例精选
Vs+Qt+C++电梯调度控制系统
如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助!
前言
这篇博客针对<<Vs+Qt+C++电梯调度控制系统>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
文章目录
一、所需工具软件
二、使用步骤
1. 引入库
2. 代码实现
3. 运行结果
三、在线协助
一、所需工具软件
1. VS,Qt
2. C++
二、使用步骤
1.引入库
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QAbstractAnimation>
#include <QTime>
#include "controller.h"
#include<QMessageBox.h>;
#include<iostream>
using namespace std;
2. 代码实现
代码如下:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
//以主界面为父窗口创建滚动条
QScrollArea* pScroll = new QScrollArea(this);
//清空数据
QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(clearData()));
QObject::connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(averTime()));
QObject::connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(personNumber()));
ui->checkBox_6->setVisible(false);
ui->checkBox_7->setVisible(false);
ui->checkBox_8->setVisible(false);
//setWindowFlags(windowFlags() &
// ~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
setWindowFlags(windowFlags() ); // 禁止最大化按钮
//setFixedSize(this->width(), this->height());
this->setWindowTitle("Elevator");
this->setWindowIcon(QIcon(":/new/image/image/little door.png"));
ctrl = new Controller();
playerList = new QMediaPlaylist;
player = new QMediaPlayer;
playerList->addMedia(QUrl("qrc:/new/image/music/ding.mp3"));
playerList->setCurrentIndex(0);
player->setPlaylist(playerList);
player->setVolume(50);
init();
setAnimation();
setConnection();
setStyleSheet(
//正常状态样式
"QPushButton{"
"border-radius:5px;"
"border:2px solid gray;"
"color:black;"
//"padding-left:8px;"
//"padding-right:8px;"
//"padding-top:8px;"
//"padding-bottom:8px;"
"}"
//鼠标悬停样式
"QPushButton:hover{"
"background-color:green;"
"color:black;"
"}"
//鼠标按下样式
"QPushButton:pressed{"
"background-color:red;"
"border:2px solid gray;"
"color:rgba(0,0,0,100);"
"}"
//设置数字显示
"QLCDNumber{"
"border:20px black;"
"color:red;"
"background:silver;"
"}"
//设置滑块显示
"QSlider::groove:vertical{"
"border: 1px solid #bbb;"
"background: white;"
"height: 802px;"
"width:50px;"
"border-radius: 4px;"
"}"
"QSlider::handle:vertical{"
"background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #eee, "
"stop:1 #ccc);"
"border: 1px solid #777;"
"border-image: url(:/new/image/image/little door.png);"
"background: green;"
"border-radius:4px;"
"width: 50px;"
"height: 50px;"
"margin-top: -2px;"
"margin-bottom: -2px;"
"}");
}
void MainWindow::init() {
QList<QGraphicsView *> graphicsViewList =
ui->centralwidget->findChildren<QGraphicsView *>();
auto it = graphicsViewList.begin();
for (int i = 0; i < 5; i++) {
it++;
}
for (int i = 0; i < 5; i++) {
upDownButton[i] = *it;
it++;
}
for (int i = 0; i < 10; i++) {
door[i] = *it;
elevAnimation[i] = new QPropertyAnimation(door[i], "geometry");
it++;
}
for (int i = 0; i < ELEVATOR_NUM; i++) {
person[i] = *it;
person[i]->setVisible(false);
preAnimation[i] = new QPropertyAnimation(person[i], "geometry");
it++;
}
QList<QLCDNumber *> lcdNumberList =
ui->centralwidget->findChildren<QLCDNumber *>();
QList<QSlider *> sliderList = ui->centralwidget->findChildren<QSlider *>();
auto it_2 = sliderList.begin();
auto it_5 = lcdNumberList.begin();
QList<QCheckBox *> checkboxList =
ui->centralwidget->findChildren<QCheckBox *>();
auto it_3 = checkboxList.begin();
for (int i = 0; i < ELEVATOR_NUM; i++) {
ctrl->myElevator[i].elevatorUi = *it_2;
ctrl->myElevator[i].lock = *it_3;
ctrl->myElevator[i].elevatorLCD = *it_5;
ctrl->myElevator[i].elevatorUi->setEnabled(false);
it_2++;
it_3++;
it_5++;
}
//
//让每个电梯类的TIMESPANLINEDIT指向UI对应的LINEEDIT
for (int i = 0; i < ELEVATOR_NUM; i++) {
ctrl->myElevator[i].timeSpanLineEdit = ui->lineEdit_timespan;
ctrl->myElevator[i].timeButtontimeLineEdit = ui->lineEdit_button;
ctrl->myElevator[i].timeOpentimeLineEdit = ui->lineEdit_3;
}
ctrl->calDoorTime = *it_3;
it_3++;
ctrl->calRepairTime = *it_3;
it_3++;
ctrl->calOpenTime = *it_3;
QList<QPushButton *> buttonList =
ui->centralwidget->findChildren<QPushButton *>();
auto it_4 = buttonList.begin();
for (int i = 0; i < ELEVATOR_NUM; i++) {
for (int j = 0; j < FLOOR; j++) {
ctrl->myElevator[i].floorButton[j] = *it_4;
connect(*it_4, SIGNAL(clicked(bool)), this, SLOT(clickFloor()));
it_4++;
}
ctrl->myElevator[i].floorButton[20] = *it_4;
connect(*it_4, SIGNAL(clicked(bool)), this, SLOT(clickWarning()));
it_4++;
ctrl->myElevator[i].floorButton[21] = *it_4;
connect(*it_4, SIGNAL(clicked(bool)), this, SLOT(clickPhone()));
it_4++;
}
for (int i = 0; i < FLOOR; i++) {
for (int j = 0; j < 2; j++) {
floorButton[i][j] = *it_4;
connect(*it_4, SIGNAL(clicked(bool)), this, SLOT(clickDirection()));
it_4++;
}
}
floorButton[19][1]->setEnabled(false);
floorButton[0][0]->setEnabled(false);
for (int i = 0; i < ELEVATOR_NUM; i++) {
ctrl->myElevator[i].floorButton[22] = *it_4;
connect(*it_4, SIGNAL(clicked(bool)), this, SLOT(clickOpen()));
it_4++;
ctrl->myElevator[i].floorButton[23] = *it_4;
connect(*it_4, SIGNAL(clicked(bool)), this, SLOT(clickClose()));
it_4++;
}
}
void MainWindow::setAnimation() {
int leftoffset = 210;
int belowoffset = 20;
for (int i = 0; i < ELEVATOR_NUM; i++) {
elevAnimation[2 * i]->setDuration(1000);
elevAnimation[2 * i]->setStartValue(QRect(leftoffset+37 + 180 * i, belowoffset+45, 42, 127));
elevAnimation[2 * i]->setEndValue(QRect(leftoffset + 37 + 180 * i, belowoffset + 45, 0, 127));
elevAnimation[2 * i + 1]->setDuration(1000);
elevAnimation[2 * i + 1]->setStartValue(QRect(leftoffset + 78 + 180 * i, belowoffset + 45, 42, 127));
elevAnimation[2 * i + 1]->setEndValue(QRect(leftoffset + 120 + 180 * i, belowoffset + 45, 0, 127));
}
for (int i = 0; i < ELEVATOR_NUM; i++) {
preAnimation[i]->setDuration(1500);
preAnimation[i]->setStartValue(QRect(leftoffset + 0 + 180 * i, belowoffset + 90, 51, 111));
preAnimation[i]->setEndValue(QRect(leftoffset + 50 + 180 * i, belowoffset + 60, 51, 111));
}
}
void MainWindow::setConnection() {
connect(this, SIGNAL(warning(int)), ctrl, SLOT(warningState(int)));
connect(this, SIGNAL(phone(int)), ctrl, SLOT(phoneState(int)));
connect(this, SIGNAL(controlDoor(int, int)), ctrl,
SLOT(controlDoorState(int, int)));
connect(this, SIGNAL(inMessage(int, int)), ctrl, SLOT(distribute(int, int)));
connect(this, SIGNAL(outMessage(int, int)), ctrl,
SLOT(selectBestElev(int, int)));
connect(ctrl, SIGNAL(open(int)), this, SLOT(doorOpenAni(int)));
connect(ctrl, SIGNAL(close(int)), this, SLOT(doorCloseAni(int)));
connect(ctrl, SIGNAL(change(int, int)), this, SLOT(changeButton(int, int)));
connect(ctrl, SIGNAL(recover(int, int)), this, SLOT(recoverButton(int, int)));
}
void MainWindow::getMessage(QString objectName, int *curMessage) {
QStringList sl = objectName.split("_");
QByteArray byte = sl.at(0).toUtf8();
curMessage[0] = int(byte.at(0)) - 96;
curMessage[1] = sl.at(1).toInt();
}
void MainWindow::openDoor(int elevatorID) {
elevAnimation[2 * elevatorID]->setDirection(
QAbstractAnimation::Forward); //正向设定动画
elevAnimation[2 * elevatorID + 1]->setDirection(QAbstractAnimation::Forward);
elevAnimation[2 * elevatorID]->start();
elevAnimation[2 * elevatorID + 1]->start();
}
void MainWindow::closeDoor(int elevatorID) {
elevAnimation[2 * elevatorID]->setDirection(
QAbstractAnimation::Backward); //反向设定动画
elevAnimation[2 * elevatorID + 1]->setDirection(QAbstractAnimation::Backward);
elevAnimation[2 * elevatorID]->start();
elevAnimation[2 * elevatorID + 1]->start();
}
void MainWindow::personIn(int elevatorID) {
person[elevatorID]->setVisible(true);
setPersonTop(elevatorID);
preAnimation[elevatorID]->setDirection(QAbstractAnimation::Forward);
preAnimation[elevatorID]->start();
QTimer::singleShot(1.5 * 1000, this,
[&, elevatorID]() { setDoorTop(elevatorID); });
}
void MainWindow::personOut(int elevatorID) {
person[elevatorID]->setVisible(true);
setPersonTop(elevatorID);
preAnimation[elevatorID]->setDirection(QAbstractAnimation::Backward);
preAnimation[elevatorID]->start();
QTimer::singleShot(1 * 1000, this, [&, elevatorID]() {
person[elevatorID]->setVisible(false);
});
}
void MainWindow::setDoorTop(int elevatorID) {
door[2 * elevatorID]->raise();
door[2 * elevatorID + 1]->raise();
}
void MainWindow::setPersonTop(int elevatorID) { person[elevatorID]->raise(); }
void MainWindow::clickFloor() {
int curMessage[2];
QPushButton *button = qobject_cast<QPushButton *>(sender());
button->setStyleSheet("background-color:red;font: 9pt 'Stencil';");
getMessage(button->objectName(), curMessage);
emit inMessage(curMessage[0], curMessage[1]);
}
void MainWindow::clickDirection() {
qDebug() << "somebody press up or down bottom: ";
int curMessage[2];
int direction;
QPushButton *button = qobject_cast<QPushButton *>(sender());
button->setStyleSheet("background-color:red;font: 9pt 'Stencil';");
getMessage(button->objectName(), curMessage);
if (curMessage[0] == UPDIRECTION) {
direction = UPSTATE;
} else {
direction = DOWNSTATE;
}
floorButton[curMessage[1] - 1][direction]->setEnabled(false);
emit outMessage(direction, curMessage[1]);
}
void MainWindow::clickWarning() {
int curMessage[2];
QPushButton *button = qobject_cast<QPushButton *>(sender());
// button->setStyleSheet(
// "background-color:red;image: url(:/new/image/image/alarm bell.png);");
getMessage(button->objectName(), curMessage);
emit warning(curMessage[0]);
}
void MainWindow::clickPhone() {
int curMessage[2];
QPushButton *button = qobject_cast<QPushButton *>(sender());
getMessage(button->objectName(), curMessage);
emit phone(curMessage[0]);
}
void MainWindow::clickOpen() {
int curMessage[2];
QPushButton *button = qobject_cast<QPushButton *>(sender());
button->setStyleSheet("font: 9pt '黑体';");
getMessage(button->objectName(), curMessage);
emit controlDoor(curMessage[0], curMessage[1]);
}
void MainWindow::clickClose() {
int curMessage[2];
QPushButton *button = qobject_cast<QPushButton *>(sender());
button->setStyleSheet("font: 9pt '黑体';");
getMessage(button->objectName(), curMessage);
emit controlDoor(curMessage[0], curMessage[1]);
}
void MainWindow::doorOpenAni(int elevatorID) {
openDoor(elevatorID);
personIn(elevatorID);
player->play();
QTimer::singleShot(1 * 1000, this,
[&, elevatorID]() { closeDoor(elevatorID); });
}
void MainWindow::doorCloseAni(int elevatorID) {
openDoor(elevatorID);
personOut(elevatorID);
player->play();
QTimer::singleShot(1 * 1000, this,
[&, elevatorID]() { closeDoor(elevatorID); });
}
void MainWindow::changeButton(int elevatorID, int state) {
if (state == FREESTATE) {
upDownButton[elevatorID]->setStyleSheet(
"border-image: url(:/new/image/image/nothing button.png);");
} else if (state == UPSTATE) {
upDownButton[elevatorID]->setStyleSheet(
"border-image: url(:/new/image/image/up button.png);");
} else if (state == DOWNSTATE) {
upDownButton[elevatorID]->setStyleSheet(
"border-image: url(:/new/image/image/down button.png);");
}
}
void MainWindow::recoverButton(int direction, int floor) {
floorButton[floor - 1][direction]->setEnabled(true);
floorButton[floor - 1][direction]->setStyleSheet("border-radius:15px;"
"border:2px solid gray;"
"color:black;"
"font: 9pt 'Stencil';");
}
void MainWindow::clearData()
{
QFile file1(QString::fromStdString("outElevator.txt"));
if (file1.exists())
{
file1.remove();
}
file1.close();
QFile file2(QString::fromStdString("doorNo.txt"));
if (file2.exists())
{
file2.remove();
}
file2.close();
}
float sumData = 0;
float lineNum = 0;
void MainWindow::averTime()
{
QFile file("outElevator.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Can't open the file!" << endl;
}
//一行一行直接读取文件
while (!file.atEnd()) {
QByteArray line = file.readLine();
QString str(line);
qDebug() <<"str: "<< str;
sumData = sumData + str.toFloat();
lineNum = lineNum + 1;
}
qDebug() << "sumData: " << sumData;
qDebug() << "lineNum: " << lineNum;
file.close();
//float frameDistance2 = round(10 / (sumData / lineNum) * 100) / 100.0;
float frameDistance2 =sumData / lineNum;
qDebug() << "frameDistance2: " << frameDistance2;
QString aaa=QString("%1").arg(frameDistance2);
ui->averLineEdit->setText(aaa);
}
void MainWindow::personNumber()
{
QString doorOpenNumberLE = ui->doorOpenNumberLineEdit->text();
QFile filetxt(QString::fromStdString("doorOpenNumber.txt"));
if (filetxt.open(QIODevice::WriteOnly))
{
QTextStream out(&filetxt);
out << doorOpenNumberLE.toInt() << "\n";
out.setCodec("UTF-8");
out.flush();
}
filetxt.close();
}
MainWindow::~MainWindow() { delete ui; }
3. 运行结果
三、在线协助:
如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Qt, C++, Python入门指导
3)界面美化
4)软件制作
博主推荐文章:python人脸识别统计人数qt窗体-CSDN博客
博主推荐文章:Python Yolov5火焰烟雾识别源码分享-CSDN博客
Python OpenCV识别行人入口进出人数统计_python识别人数-CSDN博客
个人博客主页:alicema1111的博客_CSDN博客-Python,C++,网页领域博主
博主所有文章点这里:alicema1111的博客_CSDN博客-Python,C++,网页领域博主