繁体   English   中英

QString .arg函数

[英]QString .arg function

我目前正在QT中使用QStrings,似乎无法理解为什么以下代码对我不起作用。

#include <QCoreApplication>
#include <QDebug>
#include <QString>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString test;

    int x = 5;

    test.append("This is a test of the Arg Function %1").arg(x);
    qDebug() << test;
    //should output This is a test of the Arg Function 5
    return a.exec();
}

我得到的输出是:

这是对Arg函数%1的测试

显然,我期望值5代替%1,我在这里是否缺少明显的东西?

您需要test = test.append("This is a test of the Arg Function %1").arg(x); 因为arg返回新字符串。

这里的例子:

QString i;           // current file's number
QString total;       // number of files to process
QString fileName;    // current file's name

QString status = QString("Processing file %1 of %2: %3")
                .arg(i).arg(total).arg(fileName);

或像vahancho建议的那样:

test.append(QString("This is a test of the Arg Function %1").arg(x));

迈克尔·伯尔Michael Burr)建议:

test += QString("This is a test of the Arg Function %1").arg(x);

另一个没有arg

test.sprintf("This is a test of the Arg Function %d", x);

您可能会看到那个家伙。 如何格式化QString?

因为您不阅读有关QString :: arg的文档。 它返回字符串的副本。

qDebug() << test.arg(x) - it`s must work

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM