915 lines
30 KiB
C++
915 lines
30 KiB
C++
#include "bachistoricalrecords.h"
|
||
#include "ui_bachistoricalrecords.h"
|
||
#include <QFileDialog>
|
||
#include<QStandardItemModel>
|
||
#include<QMessageBox>
|
||
#include <QApplication>
|
||
#include <QWidget>
|
||
#include <QPainter>
|
||
#include <QImage>
|
||
#include <QFile>
|
||
#include <QTextDocument>
|
||
#include <QAxObject>
|
||
#include <QAxWidget>
|
||
#include<QGraphicsDropShadowEffect>
|
||
#include<QPrinter>
|
||
bachistoricalrecords::bachistoricalrecords(QWidget *parent) :
|
||
QWidget(parent),
|
||
ui(new Ui::bachistoricalrecords),
|
||
currentPage(1),
|
||
recordsPerPage(18),
|
||
totalPages(0),
|
||
currentModel(nullptr) // 初始化 currentModel
|
||
{
|
||
ui->setupUi(this);
|
||
this->setWindowFlags(Qt::FramelessWindowHint); // 隐藏标题栏
|
||
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
|
||
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||
ui->tableView->setAlternatingRowColors(true);
|
||
|
||
model = new CheckBoxTableModel2(this);
|
||
model->setTable("xijun");// 打开数据库中名为“xijun”的表
|
||
model->select();
|
||
ui->tableView->setModel(model);
|
||
theSelection = new QItemSelectionModel(model);// 关联选择模型
|
||
|
||
connect(theSelection, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
|
||
this, SLOT(on_currentChanged(QModelIndex, QModelIndex)));
|
||
|
||
ui->tableView->setSelectionModel(theSelection); // 设置选择模型
|
||
|
||
// 设置第一列的复选框初始状态
|
||
for (int i = 0; i < model->rowCount(); ++i) {
|
||
QModelIndex index = model->index(i, 0);
|
||
model->setData(index, Qt::Unchecked, Qt::CheckStateRole);
|
||
}
|
||
// 连接信号槽,当复选框状态改变时选中整行
|
||
connect(model, &CheckBoxTableModel2::dataChanged, this, &bachistoricalrecords::onDataChanged);
|
||
|
||
// 启用整行选择模式,允许多行选中
|
||
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
ui->tableView->setSelectionMode(QAbstractItemView::MultiSelection);
|
||
|
||
// 连接点击信号
|
||
connect(ui->tableView, &QTableView::clicked, this, &bachistoricalrecords::onTableViewClicked);
|
||
|
||
// 设置自定义表头
|
||
header = new CheckBoxHeader2(Qt::Horizontal, ui->tableView);
|
||
ui->tableView->setHorizontalHeader(header);
|
||
connect(header, &CheckBoxHeader2::checkStateChanged, this, &bachistoricalrecords::onHeaderCheckStateChanged);
|
||
|
||
shuaxin();
|
||
// 创建界面组件与数据模型的字段之间的数据映射
|
||
dataMapper = new QDataWidgetMapper();
|
||
dataMapper->setModel(model);// 设置数据模型
|
||
dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
|
||
|
||
m_maxtu = new maxtu;
|
||
connect(this, SIGNAL(yuan(QPixmap)), m_maxtu, SLOT(getyuan2(QPixmap)));
|
||
// connect(this, SIGNAL(lujing(QString)), m_maxtu, SLOT(getlujing2(QString)));
|
||
connect(this, SIGNAL(lujing(QString,QString)), m_maxtu, SLOT(getlujing2(QString,QString)));
|
||
ui->pushButton->setFixedSize(76, 37);
|
||
ui->pushButton_2->setFixedSize(76, 37);
|
||
ui->pushButton_4->setFixedSize(76, 37);
|
||
|
||
ui->frame->setFixedSize(170, 37);
|
||
ui->frame_2->setFixedSize(170, 37);
|
||
|
||
ui->tableView->setItemDelegateForColumn(2, &delegate);
|
||
ui->tableView->setItemDelegateForColumn(3, &delegate);
|
||
|
||
QTimer *timer;
|
||
timer = new QTimer(this);
|
||
connect(timer, &QTimer::timeout, this, [=]() {
|
||
QDateTime dateTime = QDateTime::currentDateTime();// 获取系统当前的时间
|
||
QString str = dateTime.toString("yyyy-MM-dd hh:mm:ss");// 格式化时间
|
||
// ui->label_5->setText(str);
|
||
});
|
||
timer->start(1000);
|
||
|
||
ui->tableView->setShowGrid(false);
|
||
ui->tableView->verticalHeader()->setVisible(false);
|
||
|
||
this->setWindowFlags(Qt::FramelessWindowHint); // 隐藏标题栏
|
||
delegate2 = new ButtonDelegate4;
|
||
ui->tableView->setItemDelegateForColumn(6, delegate2);
|
||
|
||
connect(delegate2, &ButtonDelegate4::deleteButtonClicked, this, &bachistoricalrecords::shanchu);
|
||
ui->tableView->setShowGrid(false);
|
||
|
||
ui->tableView->setFixedSize(1730, 760);
|
||
ui->tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
connect(ui->tableView, &QTableView::clicked, this, &bachistoricalrecords::onCellClicked);
|
||
m_date = new date;
|
||
connect(m_date, SIGNAL(kaishi(QString)), this, SLOT(getkaishi(QString)));
|
||
m_datee = new datee;
|
||
connect(m_datee, SIGNAL(kaishi(QString)), this, SLOT(getkaishii(QString)));
|
||
|
||
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
|
||
// 设置阴影距离
|
||
shadow->setOffset(0, 0);
|
||
// 设置阴影颜色
|
||
shadow->setColor(QColor(214, 214, 214));
|
||
// 设置阴影圆角
|
||
shadow->setBlurRadius(30);
|
||
// 给嵌套QWidget设置阴影
|
||
ui->frame_3->setFixedSize(1755, 907);
|
||
ui->frame_3->setGraphicsEffect(shadow);
|
||
|
||
CenteredItemDelegate2* delegate = new CenteredItemDelegate2();
|
||
ui->tableView->setItemDelegate(delegate);
|
||
for (int i = 1; i <= 7; ++i) {
|
||
ui->tableView->setColumnWidth(i, 275);
|
||
}
|
||
ui->tableView->setColumnWidth(0, 80);
|
||
ui->tableView->horizontalHeader()->setFixedHeight(40);
|
||
connect(m_maxtu, &maxtu::guanmeng, this, [=]() { mask_window.close(); });
|
||
|
||
ui->lineEdit_2->setFixedSize(56, 32);
|
||
QWidget *buttonContainer = new QWidget(this);
|
||
buttonLayout = new QHBoxLayout(buttonContainer);
|
||
ui->scrollArea->setWidget(buttonContainer);
|
||
ui->scrollArea->setWidgetResizable(true);
|
||
|
||
buttonGroup = new QButtonGroup(this);
|
||
totalPages = (model->rowCount() + recordsPerPage - 1) / recordsPerPage;
|
||
updateButtons();
|
||
|
||
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
ui->lineEdit_2->setFixedSize(56, 32);
|
||
getname();
|
||
ui->tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
|
||
}
|
||
bachistoricalrecords::~bachistoricalrecords()
|
||
{
|
||
delete ui;
|
||
}
|
||
void bachistoricalrecords::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
||
{
|
||
if (topLeft.column() == 0) {
|
||
bool checked = model->data(topLeft, Qt::CheckStateRole).toBool();
|
||
if (checked) {
|
||
// 选中整行
|
||
ui->tableView->selectRow(topLeft.row());
|
||
} else {
|
||
// 取消当前行的选中状态
|
||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||
selectionModel->select(topLeft, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
void bachistoricalrecords::onTableViewClicked(const QModelIndex &index)
|
||
{
|
||
// 如果点击的是第一列,直接返回
|
||
if (index.column() == 0) {
|
||
return;
|
||
}
|
||
|
||
|
||
// 获取复选框索引
|
||
QModelIndex checkBoxIndex = model->index(index.row(), 0);
|
||
// 切换复选框状态
|
||
bool isChecked = model->data(checkBoxIndex, Qt::CheckStateRole).toBool();
|
||
model->setData(checkBoxIndex, !isChecked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
|
||
// 确保整行选择状态与复选框状态一致
|
||
if (!isChecked) {
|
||
ui->tableView->selectRow(index.row());
|
||
} else {
|
||
ui->tableView->selectionModel()->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
|
||
}
|
||
|
||
}
|
||
|
||
void bachistoricalrecords::onHeaderCheckStateChanged(Qt::CheckState state)
|
||
{
|
||
model->setAllCheckState(state);
|
||
if (state == Qt::Checked) {
|
||
for (int i = 0; i < model->rowCount(); ++i) {
|
||
ui->tableView->selectRow(i);
|
||
ui->tableView->selectAll();
|
||
}
|
||
} else {
|
||
ui->tableView->clearSelection();
|
||
}
|
||
}
|
||
void bachistoricalrecords::getname()
|
||
{/*
|
||
QString queryString2 = QString("SELECT id FROM users");
|
||
QSqlQuery mquery2;
|
||
if (!mquery2.exec(queryString2)) {
|
||
|
||
|
||
}
|
||
while (mquery2.next()) {
|
||
QVariant value = mquery2.value(0);
|
||
ui->comboBox->addItem(value.toString());
|
||
}*/
|
||
|
||
}
|
||
void bachistoricalrecords::getshuaxin()
|
||
{
|
||
qDebug()<<"刷新了";
|
||
QTimer::singleShot(100,this,[=](){shuaxin();});
|
||
}
|
||
void bachistoricalrecords::shanchu(const QModelIndex& index)
|
||
{
|
||
qDebug() << "删除按钮被点击,行: " << index.row() << " 列: " << index.column();
|
||
|
||
int result = QMessageBox::information(NULL, "提示", "确定要删除本行内容吗", QMessageBox::Ok | QMessageBox::Cancel);
|
||
if (result == QMessageBox::Ok) {
|
||
|
||
if (model->removeRow(index.row())) {
|
||
if (model->submitAll()) {
|
||
shuaxin(); // 重置视图,强制刷新界面显示
|
||
|
||
} else {
|
||
|
||
}
|
||
} else {
|
||
|
||
}
|
||
}
|
||
}
|
||
//刷新字段
|
||
void bachistoricalrecords::shuaxin()
|
||
{
|
||
|
||
// ui->lineEdit_3->setText("2025-01-01");
|
||
|
||
// // ...
|
||
// // 获取当前日期
|
||
// QDate currentDate = QDate::currentDate();
|
||
// // 日期加一天
|
||
// QDate nextDay = currentDate.addDays(1);
|
||
// // 将加一天后的日期以 "yyyy-MM-dd" 格式转为字符串并设置到 lineEdit_4 中
|
||
// ui->lineEdit_4->setText(nextDay.toString("yyyy-MM-dd"));
|
||
|
||
|
||
// QSqlQuery query;
|
||
|
||
// QString str=QString("SELECT * FROM xijun WHERE time BETWEEN '%1' AND '%2' ")
|
||
|
||
// .arg(ui->lineEdit_3->text())
|
||
// .arg(ui->lineEdit_4->text());
|
||
|
||
|
||
// qDebug() <<str;
|
||
|
||
// query.prepare(str);
|
||
// if (query.exec()) { // 确保查询执行成功
|
||
// // model = new QSqlQueryModel; // 更新 model
|
||
// model->setQuery(query); // 先设置查询,这样模型就会包含查询结果
|
||
model->setTable("xijun");
|
||
ui->tableView->setModel(model);
|
||
model->select();
|
||
|
||
ui->tableView->show();
|
||
|
||
|
||
model->setHeaderData(0, Qt::Horizontal, "序号");
|
||
model->setHeaderData(1, Qt::Horizontal, "用户");
|
||
model->setHeaderData(2, Qt::Horizontal, "原始照片");
|
||
model->setHeaderData(3, Qt::Horizontal, "结果照片");
|
||
model->setHeaderData(4, Qt::Horizontal, "计数");
|
||
model->setHeaderData(5, Qt::Horizontal, "时间");
|
||
model->setHeaderData(6, Qt::Horizontal, "操作");
|
||
|
||
qDebug() << "Model row count: " << model->rowCount();
|
||
totalPages = (model->rowCount() + recordsPerPage - 1) / recordsPerPage;
|
||
QString s = "共" + QString::number(totalPages) + "页";
|
||
ui->label_7->setText(s);
|
||
|
||
for (int i=0;i<=model->rowCount() ;i++ ) {
|
||
ui->tableView->setRowHeight(i,40);
|
||
|
||
}
|
||
|
||
for (int i=1;i<=7 ;i++ ) {
|
||
ui->tableView->setColumnWidth(i,275);
|
||
|
||
}
|
||
|
||
|
||
|
||
ui->tableView->setColumnWidth(0,80);
|
||
|
||
|
||
|
||
// } else {
|
||
|
||
// qDebug() << "Query failed:" << query.lastError().text();
|
||
// }
|
||
|
||
|
||
}
|
||
|
||
//pdf导出
|
||
void bachistoricalrecords::on_pushButton_2_clicked()
|
||
{
|
||
// QAbstractItemModel *model = ui->tableView->model();
|
||
// if (!model) {
|
||
// qDebug() << "No model set on the table view.";
|
||
// return;
|
||
// }
|
||
|
||
// // 获取选中的行
|
||
// QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||
// QModelIndexList selectedRows = selectionModel->selectedRows();
|
||
// if (selectedRows.isEmpty()) {
|
||
// qDebug() << "No rows selected.";
|
||
// return;
|
||
// }
|
||
|
||
// QString filePath = QFileDialog::getSaveFileName(this, "Export to CSV", "", "CSV Files (*.csv)");
|
||
// qDebug() << filePath;
|
||
|
||
// if (filePath.isEmpty()) {
|
||
// return;
|
||
// }
|
||
|
||
// QFile file(filePath);
|
||
// if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||
// qDebug() << "Could not open file for writing:" << file.errorString();
|
||
// return;
|
||
// }
|
||
|
||
// QTextStream out(&file);
|
||
|
||
// // 写入表头(可选),跳过第七列(索引为6)
|
||
// int columnCount = model->columnCount();
|
||
// for (int col = 0; col < columnCount; ++col) {
|
||
// if (col == 6) {
|
||
// continue;
|
||
// }
|
||
// out << model->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
|
||
// if (col < columnCount - 1 && col != 6) {
|
||
// out << ",";
|
||
// }
|
||
// }
|
||
// out << "\n";
|
||
|
||
// // 写入选中行的数据,跳过第七列(索引为6)
|
||
// for (const QModelIndex &index : selectedRows) {
|
||
// int row = index.row();
|
||
// for (int col = 0; col < columnCount; ++col) {
|
||
// if (col == 6) {
|
||
// continue;
|
||
// }
|
||
// QVariant data = model->data(model->index(row, col), Qt::DisplayRole);
|
||
// out << data.toString().replace(",", ";");
|
||
// if (col < columnCount - 1 && col != 6) {
|
||
// out << ",";
|
||
// }
|
||
// }
|
||
// out << "\n";
|
||
// }
|
||
|
||
// file.close();
|
||
// qDebug() << "Selected data exported to CSV successfully.";
|
||
// 获取选中的行
|
||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||
QModelIndexList selectedRows = selectionModel->selectedRows();
|
||
|
||
if (selectedRows.isEmpty()) {
|
||
qDebug() << "没有选中任何行";
|
||
QMessageBox::warning(this, "导出失败", "未选中任何行。");
|
||
return;
|
||
}
|
||
|
||
// 弹出文件保存对话框,获取用户选择的PDF文件路径
|
||
QString filePath = QFileDialog::getSaveFileName(this, "保存PDF文件", "", "PDF文件 (*.pdf)");
|
||
if (filePath.isEmpty()) {
|
||
qDebug() << "用户取消保存";
|
||
return;
|
||
}
|
||
|
||
// 创建QPrinter对象,设置输出格式为PDF
|
||
QPrinter printer(QPrinter::HighResolution);
|
||
printer.setOutputFormat(QPrinter::PdfFormat);
|
||
printer.setOutputFileName(filePath);
|
||
|
||
// 创建QPainter对象,用于绘制PDF内容
|
||
QPainter painter;
|
||
if (!painter.begin(&printer)) {
|
||
qDebug() << "无法创建PDF文件";
|
||
return;
|
||
}
|
||
|
||
// 列宽设置
|
||
const int columnWidths[] = {300, 450, 2800, 2800, 400, 1500}; // 一到七列宽度
|
||
int columnSpacing = 100; // 列间距
|
||
int x = 400; // 起始横坐标
|
||
int y = 400; // 起始纵坐标
|
||
int lineHeight = 150; // 文字行高
|
||
int rowHeight = 2800; // 修改行高度为3000
|
||
int rowSpacing = 300; // 行间距300
|
||
int currentPage = 1;
|
||
int totalPages = (selectedRows.size() + 3) / 4; // 一页存放四条数据,重新计算总页数
|
||
|
||
// 绘制表头
|
||
QStringList headers = {"序号", "用户", "原始照片", "结果照片", "计数", "时间"};
|
||
for (int col = 0; col < headers.size(); ++col) {
|
||
painter.drawText(x, y, columnWidths[col], lineHeight, Qt::AlignCenter, headers[col]);
|
||
x += columnWidths[col] + columnSpacing;
|
||
}
|
||
y += 500; // 表头高度
|
||
x = 400;
|
||
|
||
// 遍历选中的行,分页绘制
|
||
for (int rowIndex = 0; rowIndex < selectedRows.size(); ++rowIndex) {
|
||
const QModelIndex &index = selectedRows[rowIndex];
|
||
QStringList rowData;
|
||
for (int col = 0; col < model->columnCount(); ++col) {
|
||
QModelIndex cellIndex = model->index(index.row(), col);
|
||
rowData << model->data(cellIndex).toString();
|
||
}
|
||
|
||
// 绘制行数据
|
||
x = 400;
|
||
for (int col = 0; col < rowData.size(); ++col) {
|
||
if (col == 2 || col == 3) { // 图片列
|
||
QString imagePath = rowData[col];
|
||
QPixmap pixmap(imagePath);
|
||
if (!pixmap.isNull()) {
|
||
pixmap = pixmap.scaled(columnWidths[col], columnWidths[col],
|
||
Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||
painter.drawPixmap(x, y - lineHeight, pixmap);
|
||
}
|
||
} else {
|
||
painter.drawText(x, y, columnWidths[col], lineHeight,
|
||
Qt::AlignCenter, rowData[col]);
|
||
}
|
||
x += columnWidths[col] + columnSpacing;
|
||
}
|
||
|
||
y += rowHeight + rowSpacing; // 加上行间距
|
||
x = 400;
|
||
|
||
// 分页处理
|
||
if ((rowIndex + 1) % 4 == 0 || rowIndex == selectedRows.size() - 1) {
|
||
// 绘制页码
|
||
QString pageText = QString("第 %1 页 共 %2 页").arg(currentPage).arg(totalPages);
|
||
painter.drawText(4300, y + 100, pageText);
|
||
|
||
if (rowIndex != selectedRows.size() - 1) {
|
||
printer.newPage();
|
||
y = 400;
|
||
currentPage++;
|
||
|
||
// 重新绘制表头
|
||
x = 400;
|
||
for (int col = 0; col < headers.size(); ++col) {
|
||
painter.drawText(x, y, columnWidths[col], lineHeight,
|
||
Qt::AlignCenter, headers[col]);
|
||
x += columnWidths[col] + columnSpacing;
|
||
}
|
||
y += 500;
|
||
x = 400;
|
||
}
|
||
}
|
||
}
|
||
|
||
painter.end();
|
||
qDebug() << "PDF文件已保存到: " << filePath;
|
||
QMessageBox::information(this, "导出","导出成功");
|
||
}
|
||
|
||
void bachistoricalrecords::onCellClicked(const QModelIndex &index) {
|
||
if (!model) {
|
||
qDebug() << "Model is null!";
|
||
return;
|
||
}
|
||
// 检查是否点击了第三行(索引为 2)或第四行(索引为 3)
|
||
if (index.column() == 2 || index.column() == 3) {
|
||
int curRecNo = index.row();
|
||
QSqlRecord curRec = model->record(curRecNo);
|
||
qDebug() << "Record values:";
|
||
for (int i = 0; i < curRec.count(); ++i) {
|
||
qDebug() << curRec.fieldName(i) << ": " << curRec.value(i).toString();
|
||
}
|
||
// 获取 "yuanshi" 和 "jieguo" 字段的值
|
||
QString yuanshi = curRec.value("yuanshi").toString();
|
||
QString jieguo = curRec.value("jieguo").toString();
|
||
qDebug() << "yuanshi:" << yuanshi;
|
||
qDebug() << "jieguo:" << jieguo;
|
||
// 发送信号 lujing,传递 yuanshi 和 jieguo 的值
|
||
emit lujing(yuanshi, jieguo);
|
||
|
||
mask_window.setWindowOpacity(0.2);
|
||
mask_window.setStyleSheet("background-color: black;");
|
||
mask_window.setGeometry(0, 0, 1920, 1080);
|
||
mask_window.setWindowFlags(Qt::FramelessWindowHint);
|
||
mask_window.show();
|
||
|
||
m_maxtu->setWindowModality(Qt::ApplicationModal);
|
||
m_maxtu->show();
|
||
}
|
||
}
|
||
|
||
// void bachistoricalrecords::onCellClicked(const QModelIndex &index) {
|
||
// if (!model) {
|
||
// qDebug() << "Model is null!";
|
||
// return;
|
||
// }
|
||
// if (index.column() == 2 || index.column() == 3) {
|
||
// int curRecNo = index.row();
|
||
// QSqlRecord curRec = model->record(curRecNo);
|
||
// qDebug() << "Record values:";
|
||
// for (int i = 0; i < curRec.count(); ++i) {
|
||
// qDebug() << curRec.fieldName(i) << ": " << curRec.value(i).toString();
|
||
// }
|
||
// QString fieldName = (index.column() == 2) ? "yuanshi" : "jieguo";
|
||
// QString lu = curRec.value(fieldName).toString();
|
||
// qDebug() << "lu:" << lu;
|
||
// emit lujing(lu);
|
||
// mask_window.setWindowOpacity(0.2);
|
||
// mask_window.setStyleSheet("background-color: black;");
|
||
// mask_window.setGeometry(0, 0, 1920, 1080);
|
||
// mask_window.setWindowFlags(Qt::FramelessWindowHint);
|
||
// mask_window.show();
|
||
// m_maxtu->setWindowModality(Qt::ApplicationModal);
|
||
// m_maxtu->show();
|
||
// }
|
||
// }
|
||
// void bachistoricalrecords::onCellClicked(const QModelIndex &index) {
|
||
// if (!model) {
|
||
// qDebug() << "Model is null!";
|
||
// return;
|
||
// }
|
||
// // 检查是否点击了第三行或第四行
|
||
// if (index.row() == 2 || index.row() == 3) {
|
||
// int curRecNo = index.row();
|
||
// QSqlRecord curRec = model->record(curRecNo);
|
||
// qDebug() << "Record values:";
|
||
// for (int i = 0; i < curRec.count(); ++i) {
|
||
// qDebug() << curRec.fieldName(i) << ": " << curRec.value(i).toString();
|
||
// }
|
||
// // 获取 "yuanshi" 和 "jieguo" 字段的值
|
||
// QString yuanshiValue = curRec.value("yuanshi").toString();
|
||
// QString jieguoValue = curRec.value("jieguo").toString();
|
||
// qDebug() << "yuanshi:" << yuanshiValue;
|
||
// qDebug() << "jieguo:" << jieguoValue;
|
||
// // 发送信号 lujing 和 lujing2
|
||
// emit lujing(yuanshiValue);
|
||
// emit lujing2(jieguoValue);
|
||
// mask_window.setWindowOpacity(0.2);
|
||
// mask_window.setStyleSheet("background-color: black;");
|
||
// mask_window.setGeometry(0, 0, 1920, 1080);
|
||
// mask_window.setWindowFlags(Qt::FramelessWindowHint);
|
||
// mask_window.show();
|
||
// m_maxtu->setWindowModality(Qt::ApplicationModal);
|
||
// m_maxtu->show();
|
||
// }
|
||
// }
|
||
void bachistoricalrecords::on_pushButton_6_clicked()
|
||
{
|
||
emit fanhui();
|
||
}
|
||
|
||
void bachistoricalrecords::on_toolButton_clicked()
|
||
{
|
||
int result = QMessageBox::information(NULL, "提示", "确定要退出运行吗", QMessageBox::Ok| QMessageBox::Cancel);
|
||
if (result == QMessageBox::Ok) {
|
||
|
||
emit guan();
|
||
emit tuichu2();
|
||
QTimer::singleShot(500,this,[=](){qApp->quit();
|
||
|
||
}) ;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
void bachistoricalrecords::handleErrorAndRelease(QAxObject *workbook, QAxObject *workbooks, QAxWidget *excel) {
|
||
qDebug() << "操作出现错误,释放相关资源";
|
||
if (workbook) {
|
||
workbook->dynamicCall("Close()");
|
||
}
|
||
delete workbooks;
|
||
delete excel;
|
||
}
|
||
void bachistoricalrecords::on_pushButton_3_clicked()
|
||
{
|
||
m_date->move(200,170);
|
||
m_date->exec();
|
||
}
|
||
void bachistoricalrecords::on_pushButton_5_clicked()
|
||
{
|
||
m_datee->move(350,170);
|
||
m_datee->exec();
|
||
}
|
||
|
||
void bachistoricalrecords::getkaishi(QString str)
|
||
{
|
||
|
||
ui->lineEdit_3->setText(str);
|
||
|
||
}
|
||
void bachistoricalrecords::getkaishii(QString str)
|
||
{
|
||
|
||
ui->lineEdit_4->setText(str);
|
||
|
||
}
|
||
|
||
|
||
|
||
void bachistoricalrecords::onPageButtonClicked(int id) {
|
||
currentPage = id;
|
||
qDebug()<<id;
|
||
updatePage();
|
||
updateButtons();
|
||
}
|
||
|
||
void bachistoricalrecords::on_pushButton_10_clicked()
|
||
{
|
||
if (currentPage > 1) {
|
||
currentPage--;
|
||
updatePage();
|
||
updateButtons();
|
||
} else {
|
||
qDebug() << "已经是第一页,无法继续上一页操作";
|
||
}
|
||
}
|
||
|
||
|
||
void bachistoricalrecords::on_pushButton_9_clicked()
|
||
{
|
||
if (currentPage < totalPages) {
|
||
currentPage++;
|
||
updatePage();
|
||
updateButtons();
|
||
} else {
|
||
qDebug() << "已经是最后一页,无法继续下一页操作";
|
||
}
|
||
}
|
||
|
||
//跳转
|
||
void bachistoricalrecords::on_pushButton_11_clicked()
|
||
{
|
||
bool ok;
|
||
int page = ui->lineEdit_2->text().toInt(&ok);
|
||
if (ok) {
|
||
// 页码从 1 开始,所以减 1 转换为从 0 开始的索引
|
||
int targetPage = page ;
|
||
if (targetPage >= 0 && targetPage <= totalPages) {
|
||
currentPage = targetPage;
|
||
updatePage();
|
||
updateButtons();
|
||
} else {
|
||
qDebug() << "输入的页码超出范围,请输入 1 到 " << totalPages << " 之间的页码";
|
||
}
|
||
} else {
|
||
qDebug() << "输入的页码不合法,请输入一个有效的数字";
|
||
}
|
||
}
|
||
//重置
|
||
void bachistoricalrecords::on_pushButton_4_clicked()
|
||
{
|
||
|
||
shuaxin();
|
||
updatePage();
|
||
updateButtons();
|
||
}
|
||
//查询
|
||
void bachistoricalrecords::on_pushButton_clicked()
|
||
{
|
||
|
||
QSqlQuery query;
|
||
|
||
QString str=QString("SELECT * FROM xijun WHERE time BETWEEN '%1' AND '%2' ")
|
||
|
||
.arg(ui->lineEdit_3->text())
|
||
.arg(ui->lineEdit_4->text());
|
||
|
||
|
||
qDebug() <<str;
|
||
|
||
query.prepare(str);
|
||
if (query.exec()) { // 确保查询执行成功
|
||
|
||
model->setQuery(query); // 先设置查询,这样模型就会包含查询结果
|
||
|
||
|
||
ui->tableView->setModel(model);
|
||
ui->tableView->show();
|
||
|
||
model->setHeaderData(0, Qt::Horizontal, "序号");
|
||
model->setHeaderData(1, Qt::Horizontal, "用户");
|
||
model->setHeaderData(2, Qt::Horizontal, "原始照片");
|
||
model->setHeaderData(3, Qt::Horizontal, "结果照片");
|
||
model->setHeaderData(4, Qt::Horizontal, "计数");
|
||
model->setHeaderData(5, Qt::Horizontal, "时间");
|
||
model->setHeaderData(6, Qt::Horizontal, "操作");
|
||
|
||
totalPages = (model->rowCount() + recordsPerPage - 1) / recordsPerPage;
|
||
QString s = "共" + QString::number(totalPages) + "页";
|
||
ui->label_7->setText(s);
|
||
for (int i = 0; i <= model->rowCount(); i++) {
|
||
ui->tableView->setRowHeight(i, 40);
|
||
}
|
||
|
||
updateButtons();
|
||
|
||
qDebug() << "Model row count: " << model->rowCount();
|
||
for (int i=0;i<=model->rowCount() ;i++ ) {
|
||
ui->tableView->setRowHeight(i,40);
|
||
|
||
}
|
||
|
||
for (int i=0;i<=7 ;i++ ) {
|
||
ui->tableView->setColumnWidth(i,275);
|
||
|
||
}
|
||
ui->tableView->setColumnWidth(0,80);
|
||
} else {
|
||
|
||
qDebug() << "Query failed:" << query.lastError().text();
|
||
}
|
||
|
||
}
|
||
|
||
|
||
// void bachistoricalrecords::updateButtons()
|
||
// {
|
||
// QLayoutItem *item;
|
||
// while ((item = buttonLayout->takeAt(0)) != nullptr) {
|
||
// delete item->widget();
|
||
// delete item;
|
||
// }
|
||
|
||
// int startPage = std::max(1, currentPage - 4); // 计算起始页
|
||
// int endPage = std::min(totalPages, startPage + 9); // 计算结束页
|
||
|
||
// // 确保当前页在第五个位置
|
||
// if (endPage - startPage < 9 && currentPage - 4 > 0) {
|
||
// startPage = std::max(1, currentPage - (9 - (endPage - startPage)));
|
||
// endPage = std::min(totalPages, startPage + 9);
|
||
// }
|
||
|
||
// for (int i = startPage; i <= endPage; i++) {
|
||
// QPushButton *button = new QPushButton(QString::number(i), this);
|
||
// buttonLayout->addWidget(button);
|
||
// buttonGroup->addButton(button, i);
|
||
|
||
// // 设置当前页按钮样式
|
||
// if (i == currentPage) {
|
||
// button->setStyleSheet("QPushButton {color:rgb(0, 170, 255); }");
|
||
// } else {
|
||
// button->setStyleSheet("");
|
||
// }
|
||
|
||
|
||
// // 连接按钮点击信号到槽函数
|
||
// connect(button, &QPushButton::clicked, this, [=]() {
|
||
// currentPage = i;
|
||
// updatePage();
|
||
// updateButtons(); // 更新按钮样式
|
||
// });
|
||
// }
|
||
|
||
// for (int i = 0; i <= model->rowCount(); ++i) {
|
||
// ui->tableView->setRowHeight(i, 40);
|
||
// }
|
||
|
||
// for (int i = 0; i <= 7; ++i) {
|
||
// ui->tableView->setColumnWidth(i, 275);
|
||
// }
|
||
// ui->tableView->setColumnWidth(0, 80);
|
||
|
||
// }
|
||
// void bachistoricalrecords::updateButtons()
|
||
// {
|
||
// QLayoutItem *item;
|
||
// while ((item = buttonLayout->takeAt(0)) != nullptr) {
|
||
// delete item->widget();
|
||
// delete item;
|
||
// }
|
||
|
||
// int startPage = std::max(1, currentPage - 4); // 计算起始页
|
||
// int endPage = std::min(totalPages, startPage + 9); // 计算结束页
|
||
|
||
// // 确保当前页在第五个位置
|
||
// if (endPage - startPage < 9 && currentPage - 4 > 0) {
|
||
// startPage = std::max(1, currentPage - (9 - (endPage - startPage)));
|
||
// endPage = std::min(totalPages, startPage + 9);
|
||
// }
|
||
|
||
// int buttonCount = 0; // 用于记录按钮个数
|
||
// for (int i = startPage; i <= endPage; i++) {
|
||
// QPushButton *button = new QPushButton(QString::number(i), this);
|
||
// buttonLayout->addWidget(button);
|
||
// buttonGroup->addButton(button, i);
|
||
|
||
// // 设置当前页按钮样式
|
||
// if (i == currentPage) {
|
||
// button->setStyleSheet("QPushButton {color:rgb(0, 170, 255); }");
|
||
// } else {
|
||
// button->setStyleSheet("");
|
||
// }
|
||
|
||
// // 连接按钮点击信号到槽函数
|
||
// connect(button, &QPushButton::clicked, this, [=]() {
|
||
// currentPage = i;
|
||
// updatePage();
|
||
// updateButtons(); // 更新按钮样式
|
||
// });
|
||
// buttonCount++;
|
||
// }
|
||
|
||
// // 根据按钮个数设置 scrollArea 的宽度
|
||
// if (buttonCount == 1) {
|
||
// ui->scrollArea->setFixedWidth(30);
|
||
// } else if (buttonCount == 2) {
|
||
// ui->scrollArea->setFixedWidth(60);
|
||
// }
|
||
|
||
// for (int i = 0; i <= model->rowCount(); ++i) {
|
||
// ui->tableView->setRowHeight(i, 40);
|
||
// }
|
||
|
||
// for (int i = 0; i <= 7; ++i) {
|
||
// ui->tableView->setColumnWidth(i, 275);
|
||
// }
|
||
// ui->tableView->setColumnWidth(0, 80);
|
||
// }
|
||
void bachistoricalrecords::updateButtons()
|
||
{
|
||
QLayoutItem *item;
|
||
while ((item = buttonLayout->takeAt(0)) != nullptr) {
|
||
delete item->widget();
|
||
delete item;
|
||
}
|
||
|
||
int startPage = std::max(1, currentPage - 4); // 计算起始页
|
||
int endPage = std::min(totalPages, startPage + 9); // 计算结束页
|
||
|
||
// 确保当前页在第五个位置
|
||
if (endPage - startPage < 9 && currentPage - 4 > 0) {
|
||
startPage = std::max(1, currentPage - (9 - (endPage - startPage)));
|
||
endPage = std::min(totalPages, startPage + 9);
|
||
}
|
||
|
||
int buttonCount = 0; // 用于记录按钮个数
|
||
for (int i = startPage; i <= endPage; i++) {
|
||
QPushButton *button = new QPushButton(QString::number(i), this);
|
||
buttonLayout->addWidget(button);
|
||
buttonGroup->addButton(button, i);
|
||
|
||
// 设置当前页按钮样式
|
||
if (i == currentPage) {
|
||
button->setStyleSheet("QPushButton {color:rgb(0, 170, 255); }");
|
||
} else {
|
||
button->setStyleSheet("");
|
||
}
|
||
|
||
// 连接按钮点击信号到槽函数
|
||
connect(button, &QPushButton::clicked, this, [=]() {
|
||
currentPage = i;
|
||
updatePage();
|
||
updateButtons(); // 更新按钮样式
|
||
});
|
||
buttonCount++;
|
||
}
|
||
|
||
// 根据按钮个数设置 scrollArea 的宽度
|
||
if (buttonCount >= 1 && buttonCount <= 10) {
|
||
ui->scrollArea->setFixedWidth(buttonCount * 40);
|
||
}
|
||
|
||
for (int i = 0; i <= model->rowCount(); ++i) {
|
||
ui->tableView->setRowHeight(i, 40);
|
||
}
|
||
|
||
for (int i = 0; i <= 7; ++i) {
|
||
ui->tableView->setColumnWidth(i, 275);
|
||
}
|
||
ui->tableView->setColumnWidth(0, 80);
|
||
}
|
||
void bachistoricalrecords::updatePage()
|
||
{
|
||
if (model) {
|
||
int startIndex = (currentPage - 1) * recordsPerPage;
|
||
int endIndex = std::min(startIndex + recordsPerPage, model->rowCount());
|
||
for (int i = 0; i < model->rowCount(); ++i) {
|
||
ui->tableView->setRowHidden(i, i < startIndex || i >= endIndex);
|
||
}
|
||
for (int i = 0; i <= model->rowCount(); ++i) {
|
||
ui->tableView->setRowHeight(i, 40);
|
||
}
|
||
for (int i = 0; i <= 7; ++i) {
|
||
ui->tableView->setColumnWidth(i, 275);
|
||
}
|
||
ui->tableView->setColumnWidth(0, 80);
|
||
}
|
||
}
|