You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "serch.h"
#include "ui_serch.h"
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QDebug>
#include <QGraphicsDropShadowEffect> //设置阴影
serch::serch(QWidget *parent) : QDialog(parent),
ui(new Ui::serch)
{
ui->setupUi(this);
this->setWindowTitle("查询");
//插入图片
QPixmap tian(":/tian.png");
ui->label_4->setPixmap(tian);
//设置阴影
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
shadow->setOffset(-3, 0);
shadow->setColor(QColor("#aaffff"));
shadow->setBlurRadius(80);
ui->label_4->setGraphicsEffect(shadow);
// 确保数据库连接已经成功建立
QSqlDatabase db = QSqlDatabase::database("text01", false); // 使用已共享的数据库连接
if (!db.isOpen()) {
QMessageBox::critical(this, "错误", "数据库未连接成功!");
return;
}
this->setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
}
serch::~serch()
{
// 可以选择在这里显式关闭数据库连接
QSqlDatabase::database("text01").close();
delete ui;
}
void serch::on_pushButton_2_clicked()
{
// 清除查询条件和显示区域
ui->lineEdit->clear();
ui->textBrowser_4->clear();
ui->textBrowser_5->clear();
this->close();
}
void serch::on_pushButton_clicked()
{
// 获取用户输入的车辆号码
QString str = ui->lineEdit->text();
if (str.isEmpty()) {
QMessageBox::warning(this, "警告", "请输入车辆号码!");
return;
}
// 获取共享的数据库连接
QSqlDatabase db = QSqlDatabase::database("text01", false);
if (!db.isOpen()) {
QMessageBox::critical(this, "错误", "数据库未连接成功!");
return;
}
// 查询语句
QString temp = QString("SELECT * FROM message WHERE number='%1'").arg(str);
QSqlQuery query(db); // 使用数据库连接 db
if (query.exec(temp) && query.next()) {
// 数据库查询成功,获取并显示数据
int t = query.value(1).toInt(); // 假设字段顺序是t, m
int m = query.value(2).toInt();
QString times = QString::number(t);
QString total_time = QString::number(m);
ui->textBrowser_4->setText(times);
ui->textBrowser_5->setText(total_time);
}
else {
// 查询失败,显示警告
QMessageBox::warning(this, "警告", "车辆未进入过停车场!", QMessageBox::Yes);
ui->lineEdit->clear();
ui->textBrowser_4->clear();
ui->textBrowser_5->clear();
}
}