相信朋友们在工作中,项目的代码都会存在各种各样的错误码,每次看到错误码都需要到代码里面去查找错误码所对应的的含义,本人写了一个错误码的解析工具,供大家参考,可以很方便的利用工具找到错误码的含义所在
1、界面如下所示
2、点击OPEN按钮,打开错误码文件,可以找到所有的错误码枚举值和宏定义;
3、在CLEAR前的输入框,输入要查找的错误码,左下方就会显示相应的值
源码:
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
#include <QMainWindow>
#include <QMap>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_openButton_clicked();
void on_filterEdit_textChanged(const QString &arg1);
void on_clearButton_clicked();
private:
int findValue(const QString &value);
void addItemNewContent(const QString &name, const QString &value);
void addItemContent(const QString &name, const QString &value);
void addMapItem();
void FindNewBase(QString &line);
void FindNew(QString &line,int num);
void FindNewAdd(QString &line);
void FindEqualBase(QString &line);
void FindAddBase(QString &line);
void ParseFile(QTextStream &in);
void FindNewBaseAdd(QString &line);
void ParseLine(QString &line);
void clearWidget();
Ui::MainWindow *ui;
int mRow;
int mColumn;
QString curValue;
QMap<QString,QString> mMap;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
mRow = 0;
mColumn = 0;
ui->setupUi(this);
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setColumnWidth(0,100);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
QStringList head;
head << "Name" << "Value";
ui->tableWidget->setHorizontalHeaderLabels(head);
ui->fileEdit->setText("D:/work/ErrorCode/errcode.h");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addItemNewContent(const QString &name, const QString &value)
{
if(!mMap.contains(name))
{
return;
}
mMap[name] = value;
ui->tableWidget->insertRow(mRow);
QTableWidgetItem *item = new QTableWidgetItem(name);
ui->tableWidget->setItem(mRow, 0, item);
QTableWidgetItem *item2 = new QTableWidgetItem(value);
ui->tableWidget->setItem(mRow, 1, item2);
curValue = value;
mRow++;
}
void MainWindow::addItemContent(const QString &name, const QString &value)
{
if(mMap.contains(name))
{
return;
}
mMap[name] = value;
ui->tableWidget->insertRow(mRow);
QTableWidgetItem *item = new QTableWidgetItem(name);
ui->tableWidget->setItem(mRow, 0, item);
QTableWidgetItem *item2 = new QTableWidgetItem(value);
ui->tableWidget->setItem(mRow, 1, item2);
curValue = value;
mRow++;
}
void MainWindow::FindNewBase(QString &line)
{
QStringList list = line.split(" ", QString::SkipEmptyParts);
QString name = list[1];
QString value = list[2];
addItemContent(name,value);
}
void MainWindow::FindNew(QString &line,int num)
{
//qDebug() << line << ":" << num;
QStringList list = line.split(QRegExp("\\W+"),QString::SkipEmptyParts);
qDebug() << list;
if(list.length() != 1)
{
return;
}
QString name = list[0];
QString value = "0x"+QString::number((curValue.toInt(0,0)+1),16);
addItemContent(name,value);
}
void MainWindow::FindNewAdd(QString &line)
{
QStringList list = line.split(QRegExp("\\W+"),QString::SkipEmptyParts);
if(list.length() != 3)
{
return;
}
if(!mMap.contains(list[1]))
{
qDebug() << mMap;
return;
}
QString name = list[0];
QString value = "0x"+QString::number((mMap[list[1]].toInt(0,0)+list[2].toInt(0,0)),16);
addItemContent(name,value);
}
void MainWindow::FindEqualBase(QString &line)
{
QStringList list = line.split(QRegExp("\\W+"),QString::SkipEmptyParts);
if(list.length() != 2)
{
return;
}
if(!mMap.contains(list[1]))
{
qDebug() << mMap;
return;
}
QString name = list[0];
QString value = mMap[list[1]];//QString(mMap[list[2]].toInt(0,16)+list[4].toInt(0,16));
addItemContent(name,value);
}
void MainWindow::FindNewBaseAdd(QString &line)
{
QStringList list = line.split(QRegExp("\\W+"),QString::SkipEmptyParts);
if(list.length() != 4)
{
return;
}
if(!mMap.contains(list[2]))
{
qDebug() << mMap;
return;
}
QString name = list[1];
QString value = "0x"+QString::number((mMap[list[2]].toInt(0,0)+list[3].toInt(0,0)),16);
addItemContent(name,value);
}
void MainWindow::ParseLine(QString &line)
{
//qDebug() << line;
if(line.contains(QRegExp("#define\\s+\\w+\\s+\\dx\\d+$")))
{
FindNewBase(line);
}
else if(line.contains(QRegExp("=\\s*\\w+;$")))
{
FindEqualBase(line);
}
else if(line.contains(QRegExp("=\\s*\\w+[ ]\\+[ ]")))
{
FindNewAdd(line);
}
else if(line.contains(QRegExp("\\s*\\w+\\;$")))
{
FindNew(line,1);
}
else if(line.contains(QRegExp("^#define\\s+\\w+\\s+\\(\\w+\\+\\w+\\)$")))
{
FindNewBaseAdd(line);
}
}
void MainWindow::ParseFile(QTextStream &in)
{
QString line = in.readLine();
while(!line.isNull())
{
ui->textEdit->append(line);
ParseLine(line);
line = in.readLine();
}
}
int MainWindow::findValue(const QString &value)
{
QMapIterator<QString,QString> i(mMap);
int flag = 0;
while(i.hasNext())
{
i.next();
if(i.value() == value)
{
flag=1;
addItemNewContent(i.key(),i.value());
}
}
return flag;
}
void MainWindow::on_filterEdit_textChanged(const QString &arg1)
{
clearWidget();
if(findValue(arg1))
{
return;
}
addMapItem();
}
void MainWindow::on_openButton_clicked()
{
QString fileName;
if((ui->fileEdit->text().length()))
{
fileName = ui->fileEdit->text();
}
else
{
fileName = QFileDialog::getOpenFileName(this);
}
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
ui->fileEdit->setText(fileName);
QTextStream in(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
ParseFile(in);
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
//setCurrentFile(fileName);
//statusBar()->showMessage(tr("File loaded"), 2000);
}
void MainWindow::addMapItem()
{
QMap<QString,QString>::iterator it;
for(it=mMap.begin(); it!=mMap.end(); it++)
{
addItemNewContent(it.key(),it.value());
}
}
void MainWindow::clearWidget()
{
ui->tableWidget->setRowCount(0);
mRow = 0;
mColumn = 0;
}
void MainWindow::on_clearButton_clicked()
{
ui->filterEdit->clear();
}
<?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>517</width>
<height>347</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="fileEdit"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="openButton">
<property name="text">
<string>OPEN</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="filterEdit"/>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="clearButton">
<property name="text">
<string>CLEAR</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QTableWidget" name="tableWidget">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="columnCount">
<number>2</number>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>35</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>25</number>
</attribute>
<column/>
<column/>
</widget>
</item>
<item row="2" column="1">
<widget class="QTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>