需求:加载文件夹下的所有csv文件路径到list,进行处理
思路:线程池子线程传递数据到主线程,进行整合处理
主要技术点:json解析打包、线程池、文件操作、struct、vector、map等容器
关键代码:
步骤一:点击按钮实现文件加载到list
void ReportAnalysisTool::on_btnOpenDir_clicked() { QString dir = QFileDialog::getExistingDirectory(this, QString("Select Data File to load"), QString("D:/"), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); ui.le_path->setText(dir); if (dir.isEmpty()) return; m_fileInfoList = m_fromDataMoudle.readCsvFileList(dir, CSV_SUFFIX); QDateTime curTime = QDateTime::currentDateTime(); for (int i = 0; i < m_fileInfoList.size(); i++) { DealCsvRunnable* dealRunnable = new DealCsvRunnable(); connect(dealRunnable, SIGNAL(board_result(QJsonDocument, QString)), this, SLOT(board_result_slot(QJsonDocument,QString))); //connect(dealRunnable, SIGNAL(device_result(QJsonDocument, QString)), this, SLOT(device_result_slot(QJsonDocument, QString))); QString fileName = m_fileInfoList.at(i).absoluteFilePath(); dealRunnable->setFileName(fileName); QThreadPool::globalInstance()->start(dealRunnable, QThread::NormalPriority); } QTime t; t.start(); while (t.elapsed() < 800) { QCoreApplication::processEvents(); } getJsonData(); getBoradDeviceNGJsonData(); QThreadPool::globalInstance()->waitForDone(); qDebug() << "Deal csv times = " << curTime.msecsTo(QDateTime::currentDateTime()); updataTableWidget(m_boardAndDeviceResultCount, m_flawsTypeCountData); showBoardBarsGraph(); // 显示元件的柱状图 showDeviceBarsGraph(); // 添加缺陷类型的总结果图 // showFlawsBarsGraph(); updataFlawsGraph(); m_boardAndDeviceResultCount.clear(); }
QFileInfoList FromDataMoudle::readCsvFileList(const QString& _dir, const QString& _suffix)
{
QFileInfoList _fileInfoList;
if (_dir.isEmpty()) return _fileInfoList;
QDir folder(_dir);
if (!folder.isEmpty())
{
_fileInfoList = folder.entryInfoList(QStringList() << _suffix, QDir::Files, QDir::Time);
}
return _fileInfoList;
}
json打包:
QJsonObject jsonDeviceBoardCount;
jsonDeviceBoardCount.insert("BOARD_OK_COUNT", m_stBoardDataResultV_OK.size());
jsonDeviceBoardCount.insert("BOARD_NG_COUNT", m_stBoardDataResultV_NG.size());
jsonDeviceBoardCount.insert("BOARD_SUM_COUNT", m_stBoardDataResultV_OK.size() + m_stBoardDataResultV_NG.size());
jsonDeviceBoardCount.insert("DEVICE_OK_COUNT", m_boardAndDeviceResultCount.deviceOKCount);
jsonDeviceBoardCount.insert("DEVICE_NG_COUNT", m_boardAndDeviceResultCount.deviceNGCount);
jsonDeviceBoardCount.insert("DEVICE_SUM_COUNT", m_boardAndDeviceResultCount.deviceSumCount);
m_boardAndDeviceResultCount.clear();
m_stBoardDataResultV.clear();
QJsonObject jsonDevice;
QMap<QString, int>::iterator it = deviceerrorTypeCount.begin();
while (it != deviceerrorTypeCount.end())
{
qDebug() << it.key() + "123" << it.value();
jsonDevice.insert(it.key(), it.value());
it++;
}
QJsonObject jsonBoard;
QMap<QString, int>::iterator iter = boraderrorTypeCount.begin();
while (iter != boraderrorTypeCount.end())
{
qDebug() << iter.key() + "123" << iter.value();
jsonBoard.insert(iter.key(), iter.value());
iter++;
}
deviceerrorTypeCount.clear();
boraderrorTypeCount.clear();
QJsonObject json;
json.insert("jsonDeviceBoardCount", jsonDeviceBoardCount);
json.insert("jsonDevice", jsonDevice);
json.insert("jsonBoard", jsonBoard);
jsonDocument.setObject(json);
QString fileName = QString("%1.json").arg(m_fileName);
QFile file(fileName);
if (file.open(QIODevice::WriteOnly))
{
file.write(jsonDocument.toJson(QJsonDocument::Indented));
file.close();
}
emit board_result(jsonDocument, fileName);
json解析
void ReportAnalysisTool::board_result_slot(QJsonDocument document, QString csvFileName)
{
if (document.isEmpty())
{
return;
}
if (document.isObject())
{
QFile file(csvFileName);
if (!file.open(QIODevice::ReadOnly | QFile::Text))
{
qDebug() << "Open error"; return;
}
QJsonObject rootObj = document.object();
QJsonValue jsonDevice = rootObj.value("jsonDevice");
QJsonValue jsonBoard = rootObj.value("jsonBoard");
QJsonValue jsonDeviceBoardCount = rootObj.value("jsonDeviceBoardCount");
if (jsonDevice.type() == QJsonValue::Object)
{
QJsonObject deviceObject = jsonDevice.toObject();
QJsonObject boardObject = jsonBoard.toObject();
QJsonObject DeviceBoardCountObject = jsonDeviceBoardCount.toObject();
QJsonObject::Iterator iteratorJosn;
iteratorJosn = DeviceBoardCountObject.begin();
//NG元件总数
while (iteratorJosn != DeviceBoardCountObject.end())
{
QString key = iteratorJosn.key();
QJsonValue deviceCount = DeviceBoardCountObject.value(key);
qDebug() << "DeviceBoardCountObject ==" << key << deviceCount.toDouble();
m_deviceNGCount.insert(key, deviceCount.toDouble());
iteratorJosn++;
}
v_deviceNGCount.push_back(m_deviceNGCount);
m_deviceNGCount.clear();
//缺陷类型统计元件NG数量
iteratorJosn = deviceObject.begin();
while (iteratorJosn != deviceObject.end())
{
QString key = iteratorJosn.key();
QJsonValue deviceCount = deviceObject.value(key);
qDebug() << "deviceObject ==" << key << deviceCount.toDouble();
m_boardNGTypeCount.insert(key, deviceCount.toDouble());
iteratorJosn++;
}
v_boardNGTypeCount.push_back(m_boardNGTypeCount);
m_boardNGTypeCount.clear();
//缺陷类型统计产品NG数量
iteratorJosn = boardObject.begin();
while (iteratorJosn != boardObject.end())
{
QString key = iteratorJosn.key();
QJsonValue boardCount = boardObject.value(key);
m_deviceNGTypeCount.insert(iteratorJosn.key(), boardCount.toDouble());
iteratorJosn++;
}
v_deviceNGTypeCount.push_back(m_deviceNGTypeCount);
m_deviceNGTypeCount.clear();
}
file.close();
}
}
数据根据自己需求整合处理;