summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSamuel Gaist <samuel.gaist@edeltech.ch>2015-06-17 11:07:14 +0200
committerSamuel Gaist <samuel.gaist@edeltech.ch>2015-06-19 23:23:31 +0000
commit8481500f639e3d5e2259db57847a2e7068e30650 (patch)
tree043ab8047f0b11cd1c024b50d2ab8210538addde /src
parent94e364464ee162ac919a66a44e27ee9adda65c77 (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/doc/snippets/qstring/main.cpp13
-rw-r--r--src/corelib/tools/qstring.cpp19
2 files changed, 32 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp
index 4687d52e54..e03e705a0b 100644
--- a/src/corelib/doc/snippets/qstring/main.cpp
+++ b/src/corelib/doc/snippets/qstring/main.cpp
@@ -261,6 +261,19 @@ void Widget::argFunction()
str.arg("%1f").arg("Hello"); // returns "Hellof %2"
//! [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]
str = QString("Decimal 63 is %1 in hexadecimal")
.arg(63, 0, 16);
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index c933e261cc..bbb5647eea 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -7206,6 +7206,25 @@ QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const
difference if \a a1 contains e.g. \c{%1}:
\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
*/
/*!