一、前言
1、环境vs2013+Qt5.7.0
2、实现加工系统操作过程中的日志记录与txt文件保存
3、在QTableWiget中实现下拉框显示和存贮工艺数据
二、实现过程
直接上代码,如果你只需要操作日志,仅需要关注Logging部分即可:代码中的onAddControlToTable(index, "工艺")函数为实现添加下拉框的子函数。
void WorkstationHMI::recvLogMsgSlot(LogMsgType type, QString msg, bool popup, int processTime)
{
int index = ui.loggingWidget->rowCount();//目前行数
QString time = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
QString processTotalTime = QString::number(processTime) + " S ";
switch (type)
{
case Logging:
ui.loggingWidget->insertRow(index);
ui.loggingWidget->setItem(index, 0, new QTableWidgetItem(time));
ui.loggingWidget->setItem(index, 1, new QTableWidgetItem(msg));
ui.loggingWidget->setItem(index, 2, new QTableWidgetItem(processTotalTime));
onAddControlToTable(index, "工艺");
ui.loggingWidget->scrollToBottom();//将视图滚动到底部
ui.loggingWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui.loggingWidget->resizeColumnsToContents(); //根据内容调整列宽
ui.loggingWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚动条
break;
case Warning:
ui.loggingWidget->insertRow(index);
ui.loggingWidget->setItem(index, 0, new QTableWidgetItem(time));
ui.loggingWidget->setItem(index, 1, new QTableWidgetItem(QIcon(":/MainWindow/Resources/general/Warnning.png"), "警告"));
ui.loggingWidget->setItem(index, 2, new QTableWidgetItem(msg));
ui.loggingWidget->scrollToBottom();
ui.loggingWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui.loggingWidget->resizeColumnsToContents(); //根据内容调整列宽
ui.loggingWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚动条
break;
case Alarm:
ui.loggingWidget->insertRow(index);
ui.loggingWidget->setItem(index, 0, new QTableWidgetItem(time));
ui.loggingWidget->setItem(index, 1, new QTableWidgetItem(QIcon(":/MainWindow/Resources/general/Alarm.png"), "错误"));
ui.loggingWidget->setItem(index, 2, new QTableWidgetItem(msg));
ui.loggingWidget->scrollToBottom();
ui.loggingWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
ui.loggingWidget->resizeColumnsToContents(); //根据内容调整列宽
ui.loggingWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚动条
break;
default:
break;
}
}
我这里是针对激光加工系统的操作日志实现,其中工艺包含大量的数据包括速度、功率以及加工始末点信息等,因此为了更好的呈现实际加工的数据,对工艺卡加了QComboBox控件,实现过程如下:
void WorkstationHMI::onAddControlToTable(int rowIndex, QString GongYi)
{
QString AccDcc, isGetVel;
if (ui.SCurve->isChecked())
{
AccDcc = "S曲线";
}
else
{
AccDcc = "直线";
}
if (ui.bIsReachSpeedProcess->isChecked())
{
isGetVel = "达到速度出光";
}
else
{
isGetVel = "正常出光";
}
float Power = ui.laser_power_num->value();
float V = 0.0;
float layerNum = 1;
QString processDirection;
if (ui.ComBox->currentText() == "回转体")
{
V = ui.axisLineVel->value();
}
else if (ui.ComBox->currentText() == "单道实验")
{
V = ui.axisLineVel_One->value();
if (ui.DirectionX_One->isChecked())
{
processDirection = "X方向";
}
else
{
processDirection = "Y方向";
}
layerNum = ui.LayerCnt->value();
}
else if (ui.ComBox->currentText() == "多道实验")
{
V = ui.axisLineVel_N->value();
if (ui.DirectionX_N->isChecked())
{
processDirection = "X方向";
}
else
{
processDirection = "Y方向";
}
layerNum = ui.LayerCnt_2->value();
}
else if (ui.ComBox->currentText() == "S型双向加工")
{
V = ui.axisLineVel_S->value();
if (ui.DirectionX_S->isChecked())
{
processDirection = "X方向";
}
else
{
processDirection = "Y方向";
}
layerNum = ui.LayerCnt_S->value();
}
else if (ui.ComBox->currentText() == "分段扫描")
{
V = ui.axisLineVel_Subsection->value();
if (ui.DirectionX_Subsection->isChecked())
{
processDirection = "X方向";
}
else
{
processDirection = "Y方向";
}
layerNum = ui.LayerCnt_Subsection->value();
}
float x0, y0, z0,x1,y1,z1;
x0 = ui.axisFirstXPos->value();
y0 = ui.axisFirstYPos->value();
z0 = ui.axisFirstZPos->value();
x1 = ui.axisSecondXPos->value();
y1 = ui.axisSecondYPos->value();
z1 = ui.axisSecondZPos->value();
float Vf = ui.PowerFeederVel->value();
QComboBox *tmpCombox = new QComboBox();
tmpCombox->setView(new QListView());
tmpCombox->addItem("功率:" + QString::number(Power)+"%");
tmpCombox->addItem("速度:" + QString::number(V)+"mm/s");
tmpCombox->addItem("送粉:" + QString::number(Vf)+"r/min");
tmpCombox->addItem("方向:" + processDirection);
tmpCombox->addItem("层数:" + QString::number(layerNum));
tmpCombox->addItem("A-D:" + AccDcc);
tmpCombox->addItem("V-P:" + isGetVel);
tmpCombox->addItem("P0: (" + QString::number(x0) + "," + QString::number(y0) + "," + QString::number(z0) + " )");
tmpCombox->addItem("P1: (" + QString::number(x1) + "," + QString::number(y1) + "," + QString::number(z1) + " )");
tmpCombox->setEditable(true);
tmpCombox->lineEdit()->setAlignment(Qt::AlignCenter);
tmpCombox->lineEdit()->setReadOnly(true);
ui.loggingWidget->setCellWidget(rowIndex, 3, (QWidget*)tmpCombox);
tmpCombox->setCurrentText(GongYi);
}
需要的小伙伴主要看一下往下拉框中添加项目的部分,我这里工艺在第四列,索引到3,因此有:
ui.loggingWidget->setCellWidget(rowIndex, 3, (QWidget*)tmpCombox);
tmpCombox->setCurrentText(GongYi);
为了把数据保存到.TXT文件,进行了如下操作:
void WorkstationHMI::SaveLogMsgFile()
{
QString strFileName = QFileDialog::getSaveFileName(this, "保存操作日志", "LogMESFile\\", "*.txt");
if (strFileName.isNull())
return;
int rowCount = ui.loggingWidget->rowCount();
int colCount = ui.loggingWidget->columnCount();
QFile teachCmdWrite(strFileName);
QTextStream outWrite(&teachCmdWrite);
bool Error = teachCmdWrite.open(QIODevice::ReadWrite);
int Cnttt = 0;
QString Time, ProcessLog, totalTime, Gongyi;
for (int i = 0; i < rowCount; i++)
{
for (size_t t = 0; t < 9; t++)
{
Gongyi += ((QComboBox*)ui.loggingWidget->cellWidget(i, 3))->itemText(t)+" ";
}
Time = ui.loggingWidget->item(i, 0)->text();
ProcessLog = ui.loggingWidget->item(i, 1)->text();
totalTime = ui.loggingWidget->item(i, 2)->text();
QString tital = "工艺分组:";
QString tital2 = "操作时间:";
QString tital3 = "操作类型:";
QString tital4 = "持续时间:";
QString tital5 = "工艺条件:";
outWrite << tital << QString::number(i) << ":" << endl;
outWrite << tital2 << Time << endl;
outWrite << tital3<<ProcessLog << endl;
outWrite << tital4 << totalTime << endl;
outWrite << tital5<< Gongyi << endl;
Gongyi = ""; //每个工艺组完成后隔出一行
outWrite << endl;
}
teachCmdWrite.close();
}
数据流采用的QTextStream,如果有的小伙伴用了QDataStream,可能一直在文本文件中乱码,因为QDataStream底层用的二进制文件格式,因此要想后续在文本文件中安全的读取数据流,推荐使用QTextStream。
展示一下效果吧:
感谢阅读!!!