Men的博客

欢迎光临!

0%

QT实战文件读取与正则表达式

m_posData.clear();
m_file.seek(0);
QTextStream in(&m_file);
QRegExp regExp = this->getSplitRegExp();
QString line;
int lineCount = 0;
while (m_spinBox->value() >= lineCount) { // 文件忽略行
line = in.readLine();
lineCount++;
}
m_posData.clear();
m_dataHeaders = line.split(regExp);
if (m_dataHeaders.size() < m_AdjustNames.size()) {
m_topTableModel->clear();
m_bottomTableModel->clear();
QMessageBox::information(this, QStringLiteral(“提示”), QStringLiteral(“数据格式异常!”));
return;
}
while (!line.isNull()) {
QStringList item = line.split(regExp);
if(item.size() >= m_dataHeaders.size()) {
m_posData.append(item);
}
line = in.readLine();
}

QRegExp ImportPOSFileAdjustDialog::getSplitRegExp()
{
QStringList decollatorflags;
decollatorflags << “ “ <<”|”<<”.”<<”,”<<”:”;
QString regExpString =”[“;
QList<QAbstractButton*>list = m_decollatorGroupBox->buttons();
for(int i =0 ;i<list.length();i++)
{
QAbstractButton *cheBtn = list.at(i);
if(cheBtn->isChecked())
{
regExpString += decollatorflags.at(i);
}
}
regExpString += “]”;
if (m_continuousDecollatorGroupBox->checkedButton()->text() == “合并”){
regExpString += “+”;
}
QRegExp regExp(regExpString);
return regExp;
}