【问题标题】:QString::mid() always returns the same resultQString::mid() 总是返回相同的结果
【发布时间】:2016-03-22 14:34:00
【问题描述】:

这个函数应该将一个 QString 分割成(几乎)相等的片段:

QStringList splitCell(const QString& cell) const
{
    QStringList result;

    const int currentSize = cell.size();
    const int fragmentCount = currentSize / c_maxCellSize + (currentSize % c_maxCellSize ? 1 : 0); // c_maxCellSize = 32758
    const int fragmentSize = currentSize / fragmentCount + (currentSize % fragmentCount ? 1 : 0);

    for (int fragment = 0; fragment < fragmentCount; ++fragment)
    {
        QString currentFragment = cell.mid(fragmentSize * fragment, fragmentSize);
        result << currentFragment;
    }

    return result;
}

但是如果 fragmentCount > 1 那么 currentFragment 在每次迭代中总是相同的。怎么了? 附言单元格字符串可能很大(最多 100K 个符号)

【问题讨论】:

  • 您能否提供一个最小的独立示例来重现该问题?
  • 对我来说很好用。不过,如果有多个片段,您的fragmentSize 总是c_maxCellSize,不是吗?
  • @SebastianLange 不,如果 cell.size()
  • @LibertyPaul 然后它只有一个片段
  • @SebastianLange 按计划进行。如果 cell.size() > c_maxCellSize 会拆分成更多的字符串。

标签: c++ qt qstring


【解决方案1】:

似乎是编译器问题(MS VS2008-恐龙版)。 经过几次重建后,它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    相关资源
    最近更新 更多