绪论
此篇博客为管理员登录后可看到的窗口,该窗口包含两个tab页面。
主页面包括历史记录和操作。设置页面包括数据源设置和用户设置。
源码
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
// 导出数据
void on_exportBtn_clicked();
// 连接测试
void on_linkBtn_clicked();
// 保存数据源配置
void on_saveDBBtn_clicked();
// 新增用户
void on_saveUserBtn_clicked();
// 天数选择触发事件
void on_dateNum_valueChanged(int arg1);
// 初始数据导出
void on_exportAllBtn_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "config.h"
#include "myexcel.h"
#include "mytxt.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDateTime>
#include <QStandardItemModel>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 获取数据
QDateTime loginTime = Config().Get("loginTime", "lsTime").toDateTime();
QString dbUrl = Config().Get("database", "dbUrl").toString();
QString dbUser = Config().Get("database", "dbUser").toString();
QString dbPass = Config().Get("database", "dbPass").toString();
QDate dbTime = Config().Get("database", "dbTime").toDate();
QDate start = Config().Get("exportDB", "start").toDate();
QDate end = Config().Get("exportDB", "end").toDate();
QString userName = Config().Get("user", "userName").toString();
QString userPass = Config().Get("user", "userPass").toString();
// 1、主页面
// 展示上次登录时间
if (!loginTime.isNull()) {
ui->loginTime->setText(loginTime.toString("yyyy-MM-dd hh:mm:ss"));
} else {
ui->loginTime->setText("无");
}
// 展示上次导出数据时间范围
if (!start.isNull() && !end.isNull()) {
ui->dbTime_2->setText(start.toString("yyyy-MM-dd") + " 至 " + end.toString("yyyy-MM-dd"));
ui->dbTime_3->setText(end.toString("yyyy-MM-dd") + " 至 " + end.addDays(1).toString("yyyy-MM-dd"));
// 如果导出数据时间范围大于当前日期,则设置导出按键不可用
if (end.daysTo(QDate::currentDate()) < 0 || end.addDays(1).daysTo(QDate::currentDate()) < 0) {
ui->exportBtn->setEnabled(false);
}
} else if (!dbTime.isNull()) {
ui->dbTime_2->setText("无");
ui->dbTime_3->setText(dbTime.toString("yyyy-MM-dd") + " 至 " + dbTime.addDays(1).toString("yyyy-MM-dd"));
// 如果导出数据时间范围大于当前日期,则设置导出按键不可用
if (dbTime.daysTo(QDate::currentDate()) < 0 || dbTime.addDays(1).daysTo(QDate::currentDate()) < 0) {
ui->exportBtn->setEnabled(false);
}
} else {
ui->dbTime_2->setText("无");
ui->dbTime_3->setText("无");
}
ui->dateNum->setValue(1);
// 2、配置页面
// 数据源
if (dbTime.isNull()) {
dbTime = QDate::currentDate();
}
ui->dbUrl->setText(dbUrl);
ui->dbUser->setText(dbUser);
ui->dbPass->setText(dbPass);
ui->dbTime->setDate(dbTime);
// 2、用户信息
ui->userName->setText(userName);
ui->userPass->setText(userPass);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_exportBtn_clicked()
{
ui->exportBtn->setEnabled(false);
QDateTime local(QDateTime::currentDateTime());
// QString fn = "DB_" + local.toString("yyyyMMdd_hhmmss") + ".xlsx";
QString fn = "DB_" + local.toString("yyyyMMdd_hhmmss") + ".txt";
QFileDialog fd;
QString dbUrl = Config().Get("database", "dbUrl").toString();
QString dbUser = Config().Get("database", "dbUser").toString();
QString dbPass = Config().Get("database", "dbPass").toString();
QDate dbTime = Config().Get("database", "dbTime").toDate();
QDate start = Config().Get("exportDB", "end").toDate();
int dateNum = ui->dateNum->value();
if (dbUrl.isNull() || dbUser.isNull() || dbPass.isNull()) {
QMessageBox::critical(this, tr("警告!"), tr("请先设置数据源!"));
ui->exportBtn->setEnabled(true);
return;
}
// 测试数据连接
else{
// 拼接数据源
QString dsn = "DRIVER={SQL Server};SERVER=" + dbUrl + ";DATABASE=SystemLogData;UID=" + dbUser + ";PWD=" + dbPass + ";";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "DB_LINK_0");
db.setDatabaseName(dsn);
// 测试连接
bool ok = db.open();
if (!ok) {
QMessageBox::warning(this, tr("警告!"), tr("数据库连接失败!"), QMessageBox::Ok);
db.close();
ui->exportBtn->setEnabled(true);
return;
}
else {
db.close();
}
}
if (!start.isNull()) {
// QString fileName = fd.getSaveFileName(this, tr("Save excel"), "D:\\" + fn, tr("Excel 工作簿(*.xlsx)"));
QString fileName = fd.getSaveFileName(this, tr("Save txt"), "D:\\" + fn, tr("文本文档(*.txt)"));
if (fileName.isEmpty()) {
QMessageBox::critical(this, tr("警告!"), tr("要保存的文件名为空!"));
ui->exportBtn->setEnabled(true);
return;
}
// 导出数据
// MyExcel().exportExcel(fileName, start, start.addDays(dateNum));
bool flag = MyTxt().exportTxt(fileName, start, start.addDays(dateNum));
ui->exportBtn->setEnabled(true);
if (flag) {
Config().Set("exportDB", "start", start);
Config().Set("exportDB", "end", start.addDays(dateNum));
QMessageBox::information(this, tr("OK"), tr("导出数据成功!"));
exit(0);
} else {
QMessageBox::information(this, tr("警告!"), tr("导出数据失败!"));
}
} else {
// QString fileName = fd.getSaveFileName(this, tr("Save excel"), "D:\\" + fn, tr("Excel 工作簿(*.xlsx)"));
QString fileName = fd.getSaveFileName(this, tr("Save txt"), "D:\\" + fn, tr("文本文档(*.txt)"));
if (fileName.isEmpty()) {
QMessageBox::critical(this, tr("警告!"), tr("要保存的文件名为空!"));
ui->exportBtn->setEnabled(true);
return;
}
// 导出数据
// MyExcel().exportExcel(fileName, dbTime, dbTime.addDays(dateNum));
bool flag = MyTxt().exportTxt(fileName, dbTime, dbTime.addDays(dateNum));
ui->exportBtn->setEnabled(true);
if (flag) {
Config().Set("exportDB", "start", dbTime);
Config().Set("exportDB", "end", dbTime.addDays(dateNum));
QMessageBox::information(this, tr("OK"), tr("导出数据成功!"));
QApplication* app;
app->quit();
} else {
QMessageBox::information(this, tr("警告!"), tr("导出数据失败!"));
}
}
}
void MainWindow::on_linkBtn_clicked()
{
// 来源于页面
QString dbUrl = ui->dbUrl->text().trimmed();
QString dbUser = ui->dbUser->text().trimmed();
QString dbPass = ui->dbPass->text().trimmed();
// 拼接数据源
QString dsn = "DRIVER={SQL Server};SERVER=" + dbUrl + ";DATABASE=SystemLogData;UID=" + dbUser + ";PWD=" + dbPass + ";";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "DB_LINK_0");
db.setDatabaseName(dsn);
// 测试连接
bool ok = db.open();
if (ok) {
QMessageBox::warning(this, tr(" "), tr("连接成功"), QMessageBox::Ok);
} else {
QMessageBox::warning(this, tr(" "), tr("连接失败"), QMessageBox::Ok);
}
db.close();
}
void MainWindow::on_saveDBBtn_clicked()
{
// 来源于页面
QString dbUrl = ui->dbUrl->text().trimmed();
QString dbUser = ui->dbUser->text().trimmed();
QString dbPass = ui->dbPass->text().trimmed();
QDate dbTime = ui->dbTime->date();
if (dbTime.daysTo(QDate::currentDate()) <= 0) {
QMessageBox::critical(this, tr("警告!"), tr("开始时间必须小于今天!"));
return;
}
// 保存到配置文件
Config().Set("database", "dbUrl", dbUrl);
Config().Set("database", "dbUser", dbUser);
Config().Set("database", "dbPass", dbPass);
Config().Set("database", "dbTime", dbTime);
// 初始化
QDate csh;
Config().Set("exportDB", "start", csh);
Config().Set("exportDB", "end", csh);
ui->dbTime_2->setText("无");
ui->dateNum->setValue(1);
ui->dbTime_3->setText(dbTime.toString("yyyy-MM-dd") + " 至 " + dbTime.addDays(1).toString("yyyy-MM-dd"));
QMessageBox::warning(this, tr(" "), tr("保存成功"), QMessageBox::Ok);
}
void MainWindow::on_saveUserBtn_clicked()
{
QString userName = ui->userName->text().trimmed();
QString userPass = ui->userPass->text().trimmed();
if (userName == "" || userPass == "") {
QMessageBox::critical(this, tr("警告!"), tr("密码和账号不能为空!"));
return;
}
// 保存到配置文件
Config().Set("user", "userName", userName);
Config().Set("user", "userPass", userPass);
QMessageBox::warning(this, tr(" "), tr("保存成功"), QMessageBox::Ok);
}
void MainWindow::on_dateNum_valueChanged(int arg1)
{
QDate dbTime = Config().Get("database", "dbTime").toDate();
QDate start = Config().Get("exportDB", "end").toDate();
if (!start.isNull()) {
QDate end = start.addDays(arg1);
ui->dbTime_3->setText(start.toString("yyyy-MM-dd") + " 至 " + end.toString("yyyy-MM-dd"));
if (end.daysTo(QDate::currentDate()) < 0) {
QMessageBox::critical(this, tr("警告!"), tr("日期不能大于今天!"));
ui->dateNum->setValue(arg1 + end.daysTo(QDate::currentDate()));
return;
}
} else if (!dbTime.isNull()) {
QDate end = dbTime.addDays(arg1);
ui->dbTime_3->setText(dbTime.toString("yyyy-MM-dd") + " 至 " + end.toString("yyyy-MM-dd"));
if (end.daysTo(QDate::currentDate()) < 0) {
QMessageBox::critical(this, tr("警告!"), tr("日期不能大于今天!"));
ui->dateNum->setValue(arg1 + end.daysTo(QDate::currentDate()));
return;
}
}
}
void MainWindow::on_exportAllBtn_clicked()
{
ui->exportAllBtn->setEnabled(false);
QDateTime local(QDateTime::currentDateTime());
QString fn = "DB_" + local.toString("yyyyMMdd_hhmmss") + ".txt";
QFileDialog fd;
QString dbUrl = Config().Get("database", "dbUrl").toString();
QString dbUser = Config().Get("database", "dbUser").toString();
QString dbPass = Config().Get("database", "dbPass").toString();
QDate dbTime = Config().Get("database", "dbTime").toDate();
if (dbUrl.isNull() || dbUser.isNull() || dbPass.isNull()) {
QMessageBox::critical(this, tr("警告!"), tr("请先设置数据源!"));
ui->exportAllBtn->setEnabled(true);
return;
}
// 测试数据连接
else{
// 拼接数据源
QString dsn = "DRIVER={SQL Server};SERVER=" + dbUrl + ";DATABASE=SystemLogData;UID=" + dbUser + ";PWD=" + dbPass + ";";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "DB_LINK_0");
db.setDatabaseName(dsn);
// 测试连接
bool ok = db.open();
if (!ok) {
QMessageBox::warning(this, tr("警告!"), tr("数据库连接失败!"), QMessageBox::Ok);
db.close();
ui->exportAllBtn->setEnabled(true);
return;
}
else {
db.close();
}
}
QString fileName = fd.getSaveFileName(this, tr("Save txt"), "D:\\" + fn, tr("文本文档(*.txt)"));
if (fileName.isEmpty()) {
QMessageBox::critical(this, tr("警告!"), tr("要保存的文件名为空!"));
ui->exportAllBtn->setEnabled(true);
return;
}
// 导出数据
bool flag = MyTxt().exportTxt(fileName, QDate(), dbTime);
ui->exportAllBtn->setEnabled(true);
if (flag) {
QMessageBox::information(this, tr("OK"), tr("导出数据成功!"));
} else {
QMessageBox::information(this, tr("警告!"), tr("导出数据失败!"));
}
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>565</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>781</width>
<height>531</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<property name="movable">
<bool>false</bool>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>主页面</string>
</attribute>
<widget class="QGroupBox" name="groupBox_3">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>751</width>
<height>161</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="title">
<string>历史记录</string>
</property>
<widget class="QLabel" name="label_10">
<property name="geometry">
<rect>
<x>70</x>
<y>40</y>
<width>151</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>上次登录时间:</string>
</property>
</widget>
<widget class="QLabel" name="loginTime">
<property name="geometry">
<rect>
<x>240</x>
<y>40</y>
<width>431</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="label_11">
<property name="geometry">
<rect>
<x>70</x>
<y>100</y>
<width>151</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>时间选取记录:</string>
</property>
</widget>
<widget class="QLabel" name="dbTime_2">
<property name="geometry">
<rect>
<x>240</x>
<y>100</y>
<width>431</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_4">
<property name="geometry">
<rect>
<x>10</x>
<y>190</y>
<width>751</width>
<height>281</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="title">
<string>操作</string>
</property>
<widget class="QSpinBox" name="dateNum">
<property name="geometry">
<rect>
<x>240</x>
<y>100</y>
<width>291</width>
<height>31</height>
</rect>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>30</number>
</property>
</widget>
<widget class="QPushButton" name="exportBtn">
<property name="geometry">
<rect>
<x>80</x>
<y>170</y>
<width>191</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>数据一键导出</string>
</property>
</widget>
<widget class="QLabel" name="label_7">
<property name="geometry">
<rect>
<x>60</x>
<y>40</y>
<width>151</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>本次时间选取:</string>
</property>
</widget>
<widget class="QLabel" name="dbTime_3">
<property name="geometry">
<rect>
<x>240</x>
<y>40</y>
<width>431</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="label_8">
<property name="geometry">
<rect>
<x>60</x>
<y>100</y>
<width>151</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>天数选择:</string>
</property>
</widget>
<widget class="QPushButton" name="exportAllBtn">
<property name="geometry">
<rect>
<x>450</x>
<y>170</y>
<width>191</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>初始数据导出</string>
</property>
</widget>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>设置</string>
</attribute>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>731</width>
<height>261</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="title">
<string>数据源</string>
</property>
<widget class="QLineEdit" name="dbUrl">
<property name="geometry">
<rect>
<x>260</x>
<y>30</y>
<width>411</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
</widget>
<widget class="QLineEdit" name="dbUser">
<property name="geometry">
<rect>
<x>260</x>
<y>70</y>
<width>411</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
</widget>
<widget class="QLineEdit" name="dbPass">
<property name="geometry">
<rect>
<x>260</x>
<y>110</y>
<width>411</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
<widget class="QPushButton" name="linkBtn">
<property name="geometry">
<rect>
<x>160</x>
<y>210</y>
<width>92</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>连接测试</string>
</property>
</widget>
<widget class="QPushButton" name="saveDBBtn">
<property name="geometry">
<rect>
<x>430</x>
<y>210</y>
<width>92</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>保存修改</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>100</x>
<y>30</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>IP地址:</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>100</x>
<y>70</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>账号:</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>100</x>
<y>110</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>密码:</string>
</property>
</widget>
<widget class="QLabel" name="label_9">
<property name="geometry">
<rect>
<x>100</x>
<y>160</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>开始日期:</string>
</property>
</widget>
<widget class="QDateEdit" name="dbTime">
<property name="geometry">
<rect>
<x>260</x>
<y>161</y>
<width>411</width>
<height>31</height>
</rect>
</property>
<property name="maximumDate">
<date>
<year>2999</year>
<month>12</month>
<day>31</day>
</date>
</property>
<property name="minimumDate">
<date>
<year>2019</year>
<month>6</month>
<day>1</day>
</date>
</property>
<property name="displayFormat">
<string>yyyy-MM-dd</string>
</property>
<property name="date">
<date>
<year>2019</year>
<month>6</month>
<day>1</day>
</date>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>20</x>
<y>290</y>
<width>731</width>
<height>181</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="title">
<string>用户信息</string>
</property>
<widget class="QPushButton" name="saveUserBtn">
<property name="geometry">
<rect>
<x>310</x>
<y>140</y>
<width>92</width>
<height>28</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>保存</string>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>90</x>
<y>40</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>账号:</string>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>90</x>
<y>80</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>密码:</string>
</property>
</widget>
<widget class="QLineEdit" name="userName">
<property name="geometry">
<rect>
<x>190</x>
<y>40</y>
<width>411</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
</widget>
<widget class="QLineEdit" name="userPass">
<property name="geometry">
<rect>
<x>190</x>
<y>90</y>
<width>411</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
效果图
(若有什么错误,请留言指正,3Q)