将qstringlist拆分为新行的逻辑返回

时间:2017-02-03 04:18:34

标签: c++ qt qstring

我写了以下代码:

QString Utils::getDiskSpace()
{
  static QString diskSpaceCmd(qgetenv("WINDIR") + 
    "\\system32\\wbem\\wmic logicaldisk get name, freespace, size, description /format:csv");

  QProcess proc;
  QByteArray qba;
  QString myString;
  QStringList myStringList;

  proc.start(diskSpaceCmd);

  if (proc.waitForFinished(-1))
  {
    qba = proc.readAllStandardOutput();
    qba = qba.trimmed();
    QString myString = qba;

    QStringList myStringList = myString.split("\n");

    QStringList descriptions = myStringList[0].split(",");
    descriptions[0] = descriptions[0].remove(0, descriptions[0].indexOf(" ") + 1);

    for (int index = 1; index < myStringList.length(); index++)
    {
      QStringList data = myStringList[index].split(",");
      QStringList out;
      for (int ind_2 = 0; ind_2 < data.length(); ind_2++)
        out.push_back(descriptions[ind_2] + ": " + data[ind_2]);
      QString out_str = out.join(", ");
      out_str += out.join(", ");
    }
  }

  QString diskSpace(out_str);
  return (diskSpace);
}

现在这给了我以下输出:

"diskinfo": "Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 420842713088, Name: C:, Size  : 499875049472  Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: , Name: D:, Size  :   Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 324858568704, Name: E:, Size  : 487687450624  Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: 0, Name: F:, Size  : 553459712",

我想要的预期输出是:

{
    "diskinfo": "Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 420842713088, Name: C:, Size  : 499875049472
                 Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: , Name: D:, Size  : 
                 Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 324858568704, Name: E:, Size  : 487687450624 
                 Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: 0, Name: F:, Size  : 553459712",
}

如何获得以Node开头的每一行都在新行中的预期输出?

0 个答案:

没有答案