226 lines
6.3 KiB
C++
226 lines
6.3 KiB
C++
#include "sqliteReader.h"
|
|
#include <qlabel.h>
|
|
#include <qcombobox.h>
|
|
#include <qlineedit.h>
|
|
#include <qpushbutton.h>
|
|
#include <qstandarditemmodel.h>
|
|
|
|
|
|
sqliteReader::sqliteReader(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
{
|
|
ui.setupUi(this);
|
|
readIniFile(".//config//sqliteReader.ini");
|
|
sqlController.initPara();
|
|
std::vector<std::string> res=sqlController.GetTables();
|
|
widgetInit(res);
|
|
}
|
|
|
|
sqliteReader::~sqliteReader()
|
|
{
|
|
sqlController.uninitlize();
|
|
}
|
|
void sqliteReader::widgetInit(std::vector<std::string>tables)
|
|
{
|
|
//1
|
|
layout1 = new QVBoxLayout();
|
|
ui.centralWidget->setLayout(layout1);
|
|
|
|
//1-1
|
|
QWidget* vLayout2Widget = new QWidget();
|
|
QHBoxLayout * vLayoutt2 = new QHBoxLayout();
|
|
vLayout2Widget->setLayout(vLayoutt2);
|
|
layout1->addWidget(vLayout2Widget);
|
|
|
|
//1-1-1
|
|
QLabel* vLayout2Label = new QLabel(u8"请选择日期");
|
|
vLayoutt2->addWidget(vLayout2Label);
|
|
|
|
QComboBox * Layou2MBox = new QComboBox();
|
|
for (auto &data : tables)
|
|
{
|
|
Layou2MBox->addItem(QString::fromStdString(data));
|
|
}
|
|
vLayoutt2->addWidget(Layou2MBox);
|
|
connect(Layou2MBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=]() {switchWidget(Layou2MBox->currentText().toStdString()); });
|
|
|
|
QLabel* vLayout2Label2 = new QLabel(u8"根据SN查询");
|
|
vLayoutt2->addWidget(vLayout2Label2);
|
|
|
|
QLineEdit *Layout2LEdit = new QLineEdit();
|
|
vLayoutt2->addWidget(Layout2LEdit);
|
|
|
|
QPushButton* layout2QPBtn = new QPushButton(u8"查询");
|
|
vLayoutt2->addWidget(layout2QPBtn);
|
|
connect(layout2QPBtn,& QPushButton::clicked, this, [=]() {searchSN(Layou2MBox->currentText().toStdString(), Layout2LEdit->text().toStdString()); });
|
|
//1-2
|
|
tableWidget = new QTableWidget();
|
|
layout1->addWidget(tableWidget);
|
|
switchWidget(Layou2MBox->currentText().toStdString());
|
|
|
|
}
|
|
void sqliteReader::switchWidget(std::string tableName)
|
|
{
|
|
std::vector<sqlDataStruct> res = sqlController.getAllData(tableName);
|
|
tableWidget->clear();
|
|
delete tableWidget;
|
|
tableWidget = new QTableWidget();
|
|
layout1->addWidget(tableWidget);
|
|
if (res.size() > 0)
|
|
{
|
|
int colsNum = res.at(0).dataVec.size() + 1 - avoidMap.size();
|
|
tableWidget->setColumnCount(colsNum);
|
|
tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("SN"));
|
|
//for (int i = 0; i < res.at(0).dataVec.size(); i++)
|
|
{
|
|
int index = 1;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
std::unordered_map<QString, QString>::iterator it2;
|
|
|
|
for (auto&data : res.at(0).dataVec)
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.dataName));
|
|
if (it == avoidMap.end())
|
|
{
|
|
QString Name = QString::fromStdString(data.dataName);
|
|
it2 = cuttomName.find(Name);
|
|
if (it2 != cuttomName.end())
|
|
Name = it2->second;
|
|
tableWidget->setHorizontalHeaderItem(index, new QTableWidgetItem(Name));
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
//tableWidget->setColumnCount(res.size());
|
|
for (int i = 0; i < res.size(); i++)
|
|
{
|
|
int row = tableWidget->rowCount();
|
|
tableWidget->insertRow(row);
|
|
tableWidget->setItem(row, 0, new QTableWidgetItem(QString::fromStdString(res[i].SN)));
|
|
{
|
|
int index = 1;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
for (auto&data : res.at(i).dataVec)
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.dataName));
|
|
if (it == avoidMap.end())
|
|
{
|
|
tableWidget->setItem(row, index, new QTableWidgetItem(QString::fromStdString(data.valueText)));
|
|
index ++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//tableWidget->horizontalHeader
|
|
}
|
|
void sqliteReader::searchSN(std::string tableName, std::string SN)
|
|
{
|
|
sqlDataStruct res = sqlController.selectData(SN,tableName);
|
|
tableWidget->clear();
|
|
delete tableWidget;
|
|
tableWidget = new QTableWidget();
|
|
layout1->addWidget(tableWidget);
|
|
if (res.SN.compare(""))//非空
|
|
{
|
|
tableWidget->setColumnCount(res.dataVec.size() - avoidMap.size());
|
|
//tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("SN"));
|
|
{
|
|
int index = 0;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
std::unordered_map<QString, QString>::iterator it2;
|
|
for (auto&data : res.dataVec)
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.dataName));
|
|
if (it == avoidMap.end())
|
|
{
|
|
QString Name = QString::fromStdString(data.dataName);
|
|
it2 = cuttomName.find(Name);
|
|
if (it2 != cuttomName.end())
|
|
Name = it2->second;
|
|
tableWidget->setHorizontalHeaderItem(index, new QTableWidgetItem());
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
|
|
//tableWidget->setColumnCount(res.dataVec.size());
|
|
|
|
int row = tableWidget->rowCount();
|
|
tableWidget->insertRow(row);
|
|
{
|
|
int index = 0;
|
|
std::unordered_map<QString, QString>::iterator it;
|
|
for (auto&data : res.dataVec)
|
|
{
|
|
it = avoidMap.find(QString::fromStdString(data.dataName));
|
|
if (it == avoidMap.end())
|
|
{
|
|
tableWidget->setItem(row, index, new QTableWidgetItem(QString::fromStdString(data.valueText)));
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
tableWidget->setColumnCount(res.dataVec.size() + 1);
|
|
tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("SN"));
|
|
tableWidget->setColumnCount(1);
|
|
|
|
int row = tableWidget->rowCount();
|
|
tableWidget->insertRow(row);
|
|
tableWidget->setItem(row, 0, new QTableWidgetItem(u8"找不到该数据!"));
|
|
}
|
|
|
|
|
|
//tableWidget->horizontalHeader
|
|
}
|
|
|
|
bool sqliteReader::readIniFile(const QString &filePath) {
|
|
//std::unordered_map<QString, QString> avoiData;
|
|
//std::unordered_map<QString, QString> cutomName;
|
|
QFile file(filePath);
|
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
QTextStream in(&file);
|
|
QString currentSection;
|
|
|
|
while (!in.atEnd()) {
|
|
QString line = in.readLine().trimmed();
|
|
if (line.startsWith('[') && line.endsWith(']')) {
|
|
// This line is a section header
|
|
currentSection = line.mid(1, line.length() - 2);
|
|
}
|
|
else if (!line.isEmpty() && !line.startsWith(';') && !currentSection.isEmpty()) {
|
|
// This line is a key-value pair within a section
|
|
int separatorIndex = line.indexOf('=');
|
|
if (separatorIndex != -1) {
|
|
if (!currentSection.compare("avoid"))
|
|
{
|
|
QString key = line.left(separatorIndex).trimmed();
|
|
QString value = line.mid(separatorIndex + 1).trimmed();
|
|
if (!value.compare("true"))
|
|
avoidMap.insert({ key, value });
|
|
}
|
|
else if (!currentSection.compare("customName"))
|
|
{
|
|
QString key = line.left(separatorIndex).trimmed();
|
|
QString value = line.mid(separatorIndex + 1).trimmed().toUtf8();
|
|
cuttomName.insert({ key, value });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
file.close();
|
|
}
|
|
else
|
|
{
|
|
file.close();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} |