365 lines
10 KiB
C++
365 lines
10 KiB
C++
#include "setupuers.h"
|
||
#include "ui_setupuers.h"
|
||
#include<QMessageBox>
|
||
#include<QFileDialog>
|
||
setupuers::setupuers(QWidget *parent)
|
||
: QWidget(parent)
|
||
, ui(new Ui::setupuers)
|
||
{
|
||
ui->setupUi(this);
|
||
addtable();
|
||
|
||
|
||
|
||
|
||
m_addusers=new addusers();
|
||
connect(m_addusers, &addusers::xinzeng, this, &setupuers::getxinzeng);
|
||
|
||
ui->comboBox->setFixedSize(200,37);
|
||
ui->lineEdit_3->setFixedSize(157,25);
|
||
ui->lineEdit_4->setFixedSize(157,25);
|
||
ui->frame->setFixedSize(201,37);
|
||
ui->frame_2->setFixedSize(201,37);
|
||
|
||
ui->pushButton->setFixedSize(77,37);
|
||
|
||
ui->pushButton_3->setFixedSize(77,37);
|
||
ui->pushButton_4->setFixedSize(77,37);
|
||
|
||
ui->pushButton_6->setFixedSize(77,37);
|
||
ui->tableView->setShowGrid(false);
|
||
ui-> tableView->verticalHeader()->setVisible(false);
|
||
ui->tableView->setItemDelegateForColumn(5, delegate);
|
||
|
||
connect(delegate, &ButtonDelegate::editButtonClicked, this, &setupuers::bianji);
|
||
|
||
connect(delegate, &ButtonDelegate::deleteButtonClicked, this, &setupuers::shanchu);
|
||
ui->tableView->setShowGrid(false);
|
||
ui-> tableView->verticalHeader()->setVisible(false);
|
||
|
||
|
||
ui->tableView->setFixedWidth(1843);
|
||
ui->tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
CenteredItemDelegate5* delegate = new CenteredItemDelegate5();
|
||
ui->tableView->setItemDelegate(delegate);
|
||
ui->tableView->horizontalHeader()->setFixedHeight(40);
|
||
ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
for (int i=1;i<=5 ;i++ ) {
|
||
ui->tableView->setColumnWidth(i,448);
|
||
|
||
}
|
||
ui->tableView->setColumnWidth(0,50);
|
||
getshui();
|
||
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)));
|
||
|
||
}
|
||
|
||
setupuers::~setupuers()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
|
||
void setupuers::getkaishi(QString str)
|
||
{
|
||
|
||
ui->lineEdit_4->setText(str);
|
||
|
||
}
|
||
void setupuers::getkaishii(QString str)
|
||
{
|
||
|
||
ui->lineEdit_3->setText(str);
|
||
|
||
}
|
||
void setupuers::bianji(const QModelIndex& index)
|
||
{
|
||
ui->tableView->setEditTriggers(QAbstractItemView::AllEditTriggers);
|
||
qDebug() << "编辑按钮被点击,行: " << index.row() << " 列: " << index.column();
|
||
}
|
||
|
||
void setupuers::shanchu(const QModelIndex& index)
|
||
{
|
||
int result = QMessageBox::information(NULL, "提示", "确定要删除本行内容吗", QMessageBox::Ok | QMessageBox::Cancel);
|
||
if (result == QMessageBox::Ok) {
|
||
qDebug() << "删除按钮被点击,行: " << index.row() << " 列: " << index.column();
|
||
if (model->removeRow(index.row())) {
|
||
if (model->submitAll()) {
|
||
addtable(); // 重置视图,强制刷新界面显示
|
||
|
||
} else {
|
||
|
||
}
|
||
} else {
|
||
|
||
}
|
||
}
|
||
}
|
||
void setupuers::addtable()
|
||
{
|
||
model = new QSqlTableModel(this);
|
||
model->setTable("users");//打开数据库中名为“”的表
|
||
ui->tableView->setModel(model);
|
||
model->select();
|
||
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
|
||
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||
ui->tableView->setAlternatingRowColors(true);
|
||
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,"操作");
|
||
|
||
ui->tableView->show();
|
||
for (int i=0;i<=model->rowCount() ;i++ ) {
|
||
ui->tableView->setRowHeight(i,40);
|
||
|
||
}
|
||
for (int i=1;i<=5 ;i++ ) {
|
||
ui->tableView->setColumnWidth(i,448);
|
||
|
||
}
|
||
|
||
ui->tableView->setColumnWidth(0,50);
|
||
|
||
|
||
ui->tableView->setColumnHidden(2, true);
|
||
|
||
}
|
||
|
||
|
||
QString setupuers::gettime()
|
||
{
|
||
QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
|
||
QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
|
||
|
||
return str;
|
||
}
|
||
|
||
|
||
//查询
|
||
void setupuers::on_pushButton_clicked()
|
||
{
|
||
QString number1 = ui->comboBox->currentText();
|
||
QString startTime = ui->lineEdit_4->text(); // 开始时间
|
||
QString endTime = ui->lineEdit_3->text(); // 结束时间
|
||
|
||
QSqlQuery query;
|
||
QString str;
|
||
|
||
if (ui->lineEdit_4->text() == "" || ui->lineEdit_3->text() == "") {
|
||
// 如果时间输入框为空,执行原来的查询逻辑
|
||
str = QString("SELECT * FROM users WHERE id LIKE '%1%'")
|
||
.arg(number1);
|
||
} else {
|
||
// 如果时间输入框不为空,增加时间段内的检索条件
|
||
str = QString("SELECT * FROM users WHERE id LIKE '%1%' AND time BETWEEN '%2' AND '%3'")
|
||
.arg(number1)
|
||
.arg(startTime)
|
||
.arg(endTime);
|
||
}
|
||
|
||
qDebug() << str; // 输出生成的 SQL 语句,方便调试
|
||
|
||
query.prepare(str);
|
||
if (query.exec()) { // 确保查询执行成功
|
||
QSqlQueryModel *model = new QSqlQueryModel;
|
||
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, "操作");
|
||
|
||
// 设置行高
|
||
for (int i = 0; i < model->rowCount(); i++) {
|
||
ui->tableView->setRowHeight(i, 50);
|
||
}
|
||
|
||
// 设置列宽
|
||
for (int i = 1; i < model->columnCount(); i++) {
|
||
ui->tableView->setColumnWidth(i, 448);
|
||
}
|
||
ui->tableView->setColumnWidth(0, 50); // 设置第一列宽度为 50
|
||
|
||
// 隐藏密码列
|
||
ui->tableView->setColumnHidden(2, true);
|
||
|
||
} else {
|
||
qDebug() << "Query failed:" << query.lastError().text();
|
||
}
|
||
}
|
||
|
||
|
||
//重置
|
||
void setupuers::on_pushButton_6_clicked()
|
||
{
|
||
|
||
model->submitAll();
|
||
addtable();
|
||
|
||
}
|
||
|
||
|
||
//新增
|
||
void setupuers::on_pushButton_3_clicked()
|
||
{
|
||
m_addusers->setWindowModality(Qt::ApplicationModal);
|
||
|
||
|
||
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_addusers->show();
|
||
//关闭窗口
|
||
//mask_window.close();
|
||
}
|
||
void setupuers::getxinzeng()
|
||
{
|
||
qDebug()<<"xinzeng";
|
||
|
||
addtable();
|
||
mask_window.close();
|
||
}
|
||
//导出
|
||
void setupuers::on_pushButton_4_clicked()
|
||
{
|
||
// QAbstractItemModel *model = ui->tableView->model();
|
||
// if (!model) {
|
||
// qDebug() << "No model set on the table view.";
|
||
// return;
|
||
// }
|
||
// QString filePath = QFileDialog::getSaveFileName(this, "Export to CSV", "", "CSV Files (*.csv)");
|
||
|
||
// QFile file(filePath);
|
||
// if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||
// qDebug() << "Could not open file for writing:" << file.errorString();
|
||
// return;
|
||
// }
|
||
|
||
// QTextStream out(&file);
|
||
|
||
// // 写入表头(可选)
|
||
// int columnCount = model->columnCount();
|
||
// for (int col = 0; col < columnCount; ++col) {
|
||
// out << model->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
|
||
// if (col < columnCount - 1) {
|
||
// out << ",";
|
||
// }
|
||
// }
|
||
// out << "\n";
|
||
|
||
// // 写入数据行
|
||
// int rowCount = model->rowCount();
|
||
// for (int row = 0; row < rowCount; ++row) {
|
||
// for (int col = 0; col < columnCount; ++col) {
|
||
// QVariant data = model->data(model->index(row, col), Qt::DisplayRole);
|
||
// out << data.toString().replace(",", ";"); // 替换逗号以避免CSV格式问题
|
||
// if (col < columnCount - 1) {
|
||
// out << ",";
|
||
// }
|
||
// }
|
||
// out << "\n";
|
||
// }
|
||
|
||
// file.close();
|
||
// qDebug() << "Data exported to CSV successfully.";
|
||
QAbstractItemModel *model = ui->tableView->model();
|
||
if (!model) {
|
||
qDebug() << "No model set on the table view.";
|
||
return;
|
||
}
|
||
QString filePath = QFileDialog::getSaveFileName(this, "Export to CSV", "", "CSV Files (*.csv)");
|
||
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);
|
||
|
||
// 写入表头(可选)
|
||
int columnCount = model->columnCount();
|
||
for (int col = 0; col < columnCount; ++col) {
|
||
// 跳过第三列(索引为2)和第六列(索引为5)
|
||
if (col == 2 || col == 5) {
|
||
continue;
|
||
}
|
||
out << model->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
|
||
if (col < columnCount - 1 && (col!= 2 && col!= 5)) {
|
||
out << ",";
|
||
}
|
||
}
|
||
out << "\n";
|
||
|
||
// 写入数据行
|
||
int rowCount = model->rowCount();
|
||
for (int row = 0; row < rowCount; ++row) {
|
||
for (int col = 0; col < columnCount; ++col) {
|
||
// 跳过第三列(索引为2)和第六列(索引为5)
|
||
if (col == 2 || col == 5) {
|
||
continue;
|
||
}
|
||
QVariant data = model->data(model->index(row, col), Qt::DisplayRole);
|
||
out << data.toString().replace(",", ";"); // 替换逗号以避免CSV格式问题
|
||
if (col < columnCount - 1 && (col!= 2 && col!= 5)) {
|
||
out << ",";
|
||
}
|
||
}
|
||
out << "\n";
|
||
}
|
||
|
||
file.close();
|
||
qDebug() << "Data exported to CSV successfully.";
|
||
}
|
||
//返回
|
||
|
||
void setupuers::getshui()
|
||
{
|
||
ui->comboBox->clear();
|
||
|
||
QString queryString = QString("SELECT id FROM users");
|
||
QSqlQuery mquery;
|
||
if (!mquery.exec(queryString)) {
|
||
|
||
|
||
}
|
||
while (mquery.next()) {
|
||
QVariant value = mquery.value(0); // 假设要获取的列是查询结果的第一列
|
||
ui->comboBox->addItem(value.toString());
|
||
|
||
}
|
||
}
|
||
|
||
void setupuers::on_pushButton_5_clicked()
|
||
{
|
||
m_date->move(380,150);
|
||
m_date->exec();
|
||
}
|
||
|
||
|
||
void setupuers::on_pushButton_7_clicked()
|
||
{
|
||
m_datee->move(600,150);
|
||
m_datee->exec();
|
||
}
|
||
|