前面一篇文章简要介绍了车辆管理系统的界面和功能,这一篇博客只要介绍功能实现所涉及的代码,如果有人想要打包文件,我会在文章结尾附上链接供大家下载。
该系统使用了数据库存储信息,使用数据库即安全又简化了许多操作步骤,例如查询、添加、删除直接可以SQL语句,省去了很多麻烦。
我使用了MindMaster画了思维导图来简要介绍
//************连接数据库************
bool login_Management::Open_Database()
{
this->ui->lineEdit->setFocus();
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); //数据库驱动类型为SQL Server
db.setHostName("127.0.0.1"); //选择本地主机,127.0.0.1
db.setDatabaseName("Vehicle_Management");
db.setUserName("Vehicle_Management"); //登录用户
db.setPassword("123"); //密码
if(!db.open()) //打开数据库
{
qDebug()<<db.lastError().text();
QMessageBox::critical(0, QObject::tr("Database error"), db.lastError().text());
return false ; //打开失败
}
return true;
}
//登录操作
void login_Management::Login()
{
QSqlQuery mysql;
QString name=this->ui->lineEdit->text(); //读取账号
QString passwd=this->ui->lineEdit_2->text();//读取密码
QString str=QString("select * from Account where a_Id='%1' and a_Password='%2'").arg(name).arg(passwd);
mysql.exec(str); //执行SQL语句
if(name==""||passwd=="") //判断账号密码是否为空,为空则弹出提示窗
{ QMessageBox::critical(this,"登录情况","请输入账号或密码");
return ;}
if(mysql.first()) //判断查询结果是否为空,为空则不能登录
{
QMessageBox::information(this,"登录情况","登录成功");
this->hide(); //登录成功后隐藏登录窗口
if(name.at(0)=='9') //判断是否为管理员账户
{
w.setWindowTitle("车辆管理系统_管理员版");
w.show(); //进入管理员界面
}
else
{
w_2.setWindowTitle("车辆管理系统");
w_2.show(); //进入普通用户界面
}
}
else
QMessageBox::critical(this,"登录情况","密码不正确!");
}
//确认操作
void Apply_Account::clicked_Ok()//点击确认按钮后执行以下操作
{
QSqlQuery sql;
QString s;
QString Id=this->ui->lineEdit->text(); //读取申请账号
QString passwd=this->ui->lineEdit_2->text(); //读取申请密码
QString passwd2=this->ui->lineEdit_3->text(); //读取确认密码
s=QString("insert into Account values('%1','%2')").arg(Id).arg(passwd); //将创建的账户插入数据库中
if(Id.length()<3||passwd.length()<=3)//判断密码、账号是否小于3位
{ QMessageBox::critical(this,"注册情况","登录账号不小于3位,密码不能小于3位!");
return;}
if(passwd!=passwd2)
{ QMessageBox::critical(this,"提示","两次密码不同");return ;}
if(sql.exec(s))
{
QMessageBox::information(this,"注册情况","注册成功");
this->close();
}
else
QMessageBox::critical(this,"注册情况","注册失败");
}
//判断输入的内容是否符合要求
if(id.length()<1||no.length()<1)
{
QMessageBox::critical(this ,"警告","请重新输入'\n'编号和牌号都不能为空");
}
else
{
QString content="车辆编号: "+id+'\n'+"车号: "+no+'\n'+"制造方: "+company +'\n'+"购买时间: "+buy_Time+'\n'
+"型号: "+type+'\n'+"总公里数: "+total_Km+'\n'+"耗油/公里: "+oil_Consume_Km+'\n'+"过费: "+road_Toll+'\n'+
"载重: "+load_Capacity+'\n';
int ret =QMessageBox::information(this,"您将要添加的信息",content,QMessageBox::Ok|QMessageBox::Cancel ); //将添加的信息以QMessageBox形式显示出来
if(ret==QMessageBox::Ok)//如果正确则执行Add_Truck_Insert_Table函数将新消息插入数据库
{
if( Add_Truck_Insert_Table(id,no,company,buy_Time,type,total_Km,oil_Consume_Km,road_Toll,load_Capacity))
{
Add_Truck_Clear_Message();//清除输入框内容
Add_Truck_Manage_Print(id,no,company,buy_Time,type,total_Km,oil_Consume_Km,road_Toll,load_Capacity);//在表格内显示出刚插入的这条信息
}
else {
//如果插入失败提示信息
QMessageBox::critical(this ,"警告","添加失败,请重新输入");
}
}
}
//此处我用的texyChanged(Qstring) 也就是说输入栏状态一改变就触发一次查询
connect(this->ui->line_Select_Message_2,SIGNAL(textChanged(QString)),this,SLOT(Select_Car_Input()));
//此处用SQL语句中的like实现了按输入匹配查询,简单化了许多
//查询结果输出
void Select_Truck::Select_Truck_Select_Print(QString Line_Message)
{
this->model->clear();//每次查询都要清除查询信息(重点)
Select_Truck_New_Table();//表格初始化
line=0;//初始化行数
int index=this->ui->combo_Box_Select->currentIndex(); //得到选择的序号,共0、1、2、3
QString temp;//用来暂存SQL语句
if(index==0)//判断选择的序号,在选择Select的内容
{
temp=QString("select * from Truck where t_Id like '%1%'").arg(Line_Message);
}
else if(index==1)
{
temp=QString("select * from Truck where t_No like '%1%'").arg(Line_Message);
}
else if(index==2)
{
temp=QString("select * from Truck where t_Company like '%1%'").arg(Line_Message);
}
else {
temp=QString("select * from Truck where t_Buy_Time like '%1%'").arg(Line_Message);
}
QSqlQuery my_Query; //查询
if(my_Query.exec(temp))
{
//输出查询结果,从line=0开始输出
while(my_Query.next()) //一直到结尾才推出循环
{
QString id=my_Query.value(0).toString();
QString no=my_Query.value(1).toString();
QString company=my_Query.value(2).toString();
QString buy_Time=my_Query.value(3).toString();
QString type=my_Query.value(4).toString();
QString total_Km=my_Query.value(5).toString();
QString oil_Consume_Km=my_Query.value(6).toString();
QString basic_Maintenance_Fee=my_Query.value(7).toString();
QString road_Toll=my_Query.value(8).toString();
QString total_Cost=my_Query.value(9).toString();
QString load_Capacity=my_Query.value(10).toString();
//每次将一行结果输出到表格中
Select_Truck_Message_Print(id, no,company, buy_Time,type,
total_Km, oil_Consume_Km,basic_Maintenance_Fee,
road_Toll,total_Cost,load_Capacity);
}
}
else
{
Select_Truck_New_Table();//表格初始化
}
}
//确认按钮后删除信息
void Select_Truck::Delete_Truck_Message_Ok()
{
QString message=this->ui->line_Delete_Message->text();//读取文本栏的信息
int index=this->ui->combo_Box_Delete->currentIndex();//得到选择的序号
QString SQL;
QString SQL_Select;
if(index==0)
{
SQL=QString("delete from Truck where t_Id='%1'").arg(message);
SQL_Select=QString("select * from Truck where t_Id='%1'").arg(message);
}
else if(index==1)
{
SQL=QString("delete from Truck where t_No='%1'").arg(message);
SQL_Select=QString("select * from Truck where t_No='%1'").arg(message);
}
else if(index==2)
{
SQL=QString("delete from Truck where t_Company='%1'").arg(message);
SQL_Select=QString("select * from Truck where t_Company='%1'").arg(message);
}
else {
SQL=QString("delete from Truck where t_Buy_Time='%1'").arg(message);
SQL_Select=QString("select * from Truck where t_Buy_Time='%1'").arg(message);
}
QSqlQuery my_Delete; //定义执行SQL语句变量
QSqlQuery my_Select; //查询是否存在所要删除信息
my_Select.exec(SQL_Select);
if(my_Select.first()) //********有些问题,提示不完全*********
{
int ret =QMessageBox::information(this,"是否删除","请谨慎考虑",QMessageBox::Ok|QMessageBox::Cancel );
if(ret==QMessageBox::Ok)
{
my_Delete.exec(SQL);//执行SQL删除语句
QMessageBox::information(this ,"提示","删除成功");
QString Line_Message=ui->line_Select_Message->text();//读取文本框内信息
Select_Truck_Select_Print(Line_Message);//执行一次查询线束删除后信息
}
else return ;
}
else
{
QMessageBox::critical(this ,"提示","删除信息失败");
}
}
//***********更新学生信息************
//确认按钮后进行更新
void Select_Truck::Update_Truck_Message_Ok()
{
QString message=this->ui->line_Update_Message->text();
int index=this->ui->combo_Box_Update->currentIndex();
QString select_Message; //存入被修改人的信息
QString SQL;
if(index==0)//选择按什么搜索被更新人信息
{
select_Message="t_Id";
}
else if(index==1)
{
select_Message="t_No";
}
else if(index==2)
{
select_Message="t_Company";
}
else {
select_Message="t_Buy_Time";
}
if(message!="")
{
Update_Truck_Message_Judge(select_Message,message);
}
else {
QMessageBox::critical(this ,"警告","修改失败,请重新输入");
}
}
//判断需要改变什么
void Select_Truck::Update_Truck_Message_Judge(QString select_Message,QString message)
{
QString SQL_Update; //赋值SQL执行语句
QString SQL;
QSqlQuery my_Update; //定义SQL执行变量
SQL=QString("select * from Truck where %1='%2'").arg(select_Message).arg(message);
my_Update.exec(SQL);
if(!my_Update.first())
{
QMessageBox::critical(this ,"警告","修改失败,请重新输入");
return ;
}
int ret =QMessageBox::information(this,"是否更新","请谨慎考虑",QMessageBox::Ok|QMessageBox::Cancel );
if(ret!=QMessageBox::Ok)
{
return ;
}
//获得需要改变的状态
bool index_No=this->ui->check_No_1->isChecked();
bool index_Company=this->ui->check_Company_1->isChecked();
bool index_Buy_Time=this->ui->check_Buy_Time_1->isChecked();
bool index_Type=this->ui->check_Type_1->isChecked();
bool index_Total_Km=this->ui->check_Total_Km_1->isChecked();
bool index_Consum_Km=this->ui->check_Oil_Consum_Km_1->isChecked();
bool index_Basic_Maintenance_Fee=this->ui->check_Basic_Maintenance_Fee_1->isChecked();
bool index_Road_Toll=this->ui->check_Road_Toll_1->isChecked();
bool index_Load_Capacity=this->ui->check_Load_Capacity_1->isChecked();
//获得文本框信息
QString line_No=this->ui->line_No_1->text();
QString line_Company=this->ui->line_Company_1->text();
QString line_Buy_Time=this->ui->line_Buy_Time_1->text();
QString line_Type=this->ui->line_Type_1->text();
QString line_Total_Km=this->ui->line_Total_Km_1->text();
QString line_Consume_Km=this->ui->line_Oil_Consume_Km_1->text();
QString line_Basic_Maintenance_Fee=this->ui->check_Basic_Maintenance_Fee_1->text();
QString line_Road_Toll=this->ui->line_Road_Toll_1->text();
QString line_Load_Capacity=this->ui->line_Load_Capacity_1->text();
//判断并进行SQL语句
QString line_Id=this->ui->line_Update_Message->text();
if(index_No==1)
{
if(line_No.length()<1)
{
QMessageBox::critical(this ,"警告","修改失败,车号不能为空");
return;
}
SQL_Update=QString("update Truck set t_No='%1' where %2='%3'").arg(line_No).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Company==1)
{
SQL_Update=QString("update Truck set t_Company='%1' where %2='%3'").arg(line_Company).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Road_Toll==1)
{
SQL_Update=QString("update Truck set t_Road_Toll='%1' where %2='%3'").arg(line_Road_Toll).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Buy_Time==1)
{
SQL_Update=QString("update Truck set t_Buy_Time='%1' where %2='%3'").arg(line_Buy_Time).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Type==1)
{
SQL_Update=QString("update Truck set t_Type='%1' where %2='%3'").arg(line_Type).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Total_Km==1)
{
SQL_Update=QString("update Truck set t_Total_Km='%1' where %2='%3'").arg(line_Total_Km).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Consum_Km==1)
{
SQL_Update=QString("update Truck set t_Consum_Km='%1' where %2='%3'").arg(line_Consume_Km).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Basic_Maintenance_Fee==1)
{
SQL_Update=QString("update Truck set t_Basic_Maintenance_Fee='%1' where %2='%3'").arg(line_Basic_Maintenance_Fee).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
if(index_Load_Capacity==1)
{
SQL_Update=QString("update Truck set t_Load_Capacity='%1' where %2='%3'").arg(line_Load_Capacity).arg(select_Message).arg(message);
my_Update.exec(SQL_Update);
}
QString Line_Message=ui->line_Select_Message->text();//读取文本框内信息
//Select_Truck_Select_Print(Line_Message);
Select_Truck_Select_Update(); //更新后显示结果
QMessageBox::information(this ,"提示","修改成功");
Select_Truck_Clear_Message();//清除修改输入框内容
}
//这是实现的槽函数,点击触发和两窗口间信息传送
connect(this->ui->table_Message, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(Click_On_Table_Message(const QModelIndex &)));//表格点击触发
connect(this,SIGNAL(Send_Message(QString ,QString,QString)),add,SLOT(Receive_Message(QString ,QString,QString )));//发送接受信息
//通过双击获得表格信息,如果不存在司机则弹出添加窗口供管理员添加司机信息
void Select_Truck::Click_On_Table_Message(const QModelIndex &index)
{
QString cellText = index.data().toString();
int row=index.row();//选中的行号
if (index.isValid())
{
QModelIndex dex=model->index(row,0);//获取车辆编号
QString vehicle_Id=model->data(dex).toString();
//qDebug()<
QString select=QString("select * from Driver where d_Id ='%1'").arg(vehicle_Id);
QSqlQuery my_Query; //查询
my_Query.exec(select);
if(my_Query.first())
{
QString name=my_Query.value(3).toString();
QString gender=my_Query.value(4).toString();
QString fee_Km=my_Query.value(5).toString();
QString salary=my_Query.value(8).toString();
//每次将一行结果输出到表格中
QString content="姓名: "+name+'\n'+"性别: "+gender+'\n'+"费用/公里: "+fee_Km+'\n'+"工资: "+salary+'\n';
int ret =QMessageBox::information(this,"司机信息",content,QMessageBox::Ok);
// qDebug()<<"测试"<<" ";
}
else
{
//获取一行信息的代码,此处只需要获取编号
QModelIndex dex0=model->index(row,0); //model 是事先与tableView绑定好的
QString id=model->data(dex0).toString();
QModelIndex dex1=model->index(row,1); //model 是事先与tableView绑定好的
QString no=model->data(dex1).toString();
QModelIndex dex2=model->index(row,4); //model 是事先与tableView绑定好的
QString type=model->data(dex2).toString();
// qDebug()<
emit Send_Message(id,no,type);//向Add_Driver发送信息
add->exec();
}
}
总体上就这么多功能,其他没列出的都和这些相似,如果觉得我的样式表比较好看可以去我的下一篇博客,我会把具体实现的QT样式表一一列举出来
上一篇 QT+SQL Server实现车辆管理系统 -简介
下一篇 QT+SQL Server实现车辆管理系统 -QT样式表
车辆管理系统源代码下载链接,O(∩_∩)O~