arg的这种用法好吗?

时间:2016-02-05 22:06:24

标签: qt parameter-passing qstring

msg.append(QString("\"settime\": %1000,")  .arg(eventList[i].failureBegin)); // set time

我想知道是否可以将%1放在000旁边。因为只有1个参数,所以QString显然没有混淆但是如果我有10个.arg()那么它会将它与%10混淆吧?是否有一个转义序列或我是否必须将其分解为连接?

1 个答案:

答案 0 :(得分:2)

不行。您可以浏览source code of QString::arg或让我告诉您。

QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
{
    ArgEscapeData d = findArgEscapes(*this);
    if (d.occurrences == 0) {
        qWarning() << ""QString::arg: Argument missing:"" << *this << ',' << a;
        return *this;
    }
    //.... something not important.
}

这就是findArgEscapes的实施方式。我认为Qt的源代码比大多数STL实现更具可读性。

static ArgEscapeData findArgEscapes(const QString &s)
{
    const QChar *uc_begin = s.unicode();
    const QChar *uc_end = uc_begin + s.length();
    ArgEscapeData d;
    d.min_escape = INT_MAX;
    d.occurrences = 0;
    d.escape_len = 0;
    d.locale_occurrences = 0;
    const QChar *c = uc_begin;
    while (c != uc_end) {
        while (c != uc_end && c->unicode() != '%')
            ++c;
        if (c == uc_end)
            break;
        const QChar *escape_start = c;
        if (++c == uc_end)
            break;
        bool locale_arg = false;
        if (c->unicode() == 'L') {
            locale_arg = true;
            if (++c == uc_end)
                break;
        }

        int escape = c->digitValue();
        if (escape == -1)
            continue;
        ++c;
        if (c != uc_end) {
            int next_escape = c->digitValue();
            if (next_escape != -1) {
                escape = (10 * escape) + next_escape;  //*************
                ++c;
            }
        }
        if (escape > d.min_escape)
            continue;
        if (escape < d.min_escape) {
            d.min_escape = escape;
            d.occurrences = 0;
            d.escape_len = 0;
            d.locale_occurrences = 0;
        }
        ++d.occurrences;
        if (locale_arg)
            ++d.locale_occurrences;
        d.escape_len += c - escape_start;
    }
    return d;
}