Improve QString doc when using non-spaced numbered place marker

Currently when a developer uses a string like
QString("%1%3%2").arg(x).arg(y).arg(z) he can be bitten by the
sequential replacement done by QString. Adding an example with a little
explanation should help future Qt user avoid generating buggy strings.

Task-number: QTBUG-44044
Change-Id: I81e20af8d9fb2a07e12ec61dcd5bb4544d863777
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
Samuel Gaist 2015-06-17 11:07:14 +02:00
parent 94e364464e
commit 8481500f63
2 changed files with 32 additions and 0 deletions

View File

@ -261,6 +261,19 @@ void Widget::argFunction()
str.arg("%1f").arg("Hello"); // returns "Hellof %2" str.arg("%1f").arg("Hello"); // returns "Hellof %2"
//! [13] //! [13]
//! [97]
str = "%1%3%2";
str.arg("Hello").arg(20).arg(50); // returns "Hello500"
str = "%1%2%3";
str.arg("Hello").arg(50).arg(20); // returns "Hello5020"
//! [97]
//! [98]
str = "%1%2%3";
str.arg("Hello", QString::number(20), QString::number(50)); // returns "Hello5020"
//! [98]
//! [14] //! [14]
str = QString("Decimal 63 is %1 in hexadecimal") str = QString("Decimal 63 is %1 in hexadecimal")
.arg(63, 0, 16); .arg(63, 0, 16);

View File

@ -7206,6 +7206,25 @@ QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const
difference if \a a1 contains e.g. \c{%1}: difference if \a a1 contains e.g. \c{%1}:
\snippet qstring/main.cpp 13 \snippet qstring/main.cpp 13
A similar problem occurs when the numbered place markers are not
white space separated:
\snippet qstring/main.cpp 12
\snippet qstring/main.cpp 97
Let's look at the substitutions:
\list
\li First, \c Hello replaces \c {%1} so the string becomes \c {"Hello%3%2"}.
\li Then, \c 20 replaces \c {%2} so the string becomes \c {"Hello%320"}.
\li Since the maximum numbered place marker value is 99, \c 50 replaces \c {%32}.
\endlist
Thus the string finally becomes \c {"Hello500"}.
In such cases, the following yields the expected results:
\snippet qstring/main.cpp 12
\snippet qstring/main.cpp 98
*/ */
/*! /*!