緒論
此篇博客爲管理員登錄後可看到的窗口,該窗口包含兩個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)