How to deal with “%1” in the argument of QString::arg()?

前端 未结 3 585
花落未央
花落未央 2020-12-09 02:36

Everybody loves

QString(\"Put something here %1 and here %2\")
    .arg(replacement1)
    .arg(replacement2);

but things get itchy as soon

相关标签:
3条回答
  • 2020-12-09 02:54

    See the Qt docs about QString::arg():

    QString str;
    str = "%1 %2";
    str.arg("%1f", "Hello"); // returns "%1f Hello"
    
    0 讨论(0)
  • 2020-12-09 03:00

    Note that the arg() overload for multiple arguments only takes QString. In case not all the arguments are QStrings, you could change the order of the placeholders in the format string:

    QString("1%1 2%2 3%3 4%4").arg(int1).arg(string2).arg(string3).arg(int4);
    

    becomes

    QString("1%1 2%3 3%4 4%2").arg(int1).arg(int4).arg(string2, string3);
    

    That way, everything that is not a string is replaced first, and then all the strings are replaced at the same time.

    0 讨论(0)
  • 2020-12-09 03:01

    You should try using

    QString("%1-%2").arg("%2","foo");
    
    0 讨论(0)
提交回复
热议问题