summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp5
-rw-r--r--src/corelib/global/qglobal.cpp23
-rw-r--r--src/corelib/global/qglobal.h9
-rw-r--r--tests/auto/corelib/tools/qstring/tst_qstring.cpp18
4 files changed, 46 insertions, 9 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
index 6ff4f57945..ccf8399e0d 100644
--- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp
@@ -439,6 +439,11 @@ qWarning("%s: %s", qUtf8Printable(key), qUtf8Printable(value));
//! [37]
+//! [qUtf16Printable]
+qWarning("%ls: %ls", qUtf16Printable(key), qUtf16Printable(value));
+//! [qUtf16Printable]
+
+
//! [38]
struct Point2D
{
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp
index c1ab2d69fa..9182529e08 100644
--- a/src/corelib/global/qglobal.cpp
+++ b/src/corelib/global/qglobal.cpp
@@ -3825,6 +3825,29 @@ int qrand()
*/
/*!
+ \macro const wchar_t *qUtf16Printable(const QString &str)
+ \relates <QtGlobal>
+ \since 5.7
+
+ Returns \a str as a \c{const ushort *}, but cast to a \c{const wchar_t *}
+ to avoid warnings. This is equivalent to \a{str}.utf16() plus some casting.
+
+ The only useful thing you can do with the return value of this macro is to
+ pass it to QString::asprintf() for use in a \c{%ls} conversion. In particular,
+ the return value is \e{not} a valid \c{const wchar_t*}!
+
+ In general, the pointer will be invalid after the statement in which
+ qUtf16Printable() is used. This is because the pointer may have been
+ obtained from a temporary expression, which will fall out of scope.
+
+ Example:
+
+ \snippet code/src_corelib_global_qglobal.cpp qUtf16Printable
+
+ \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal()
+*/
+
+/*!
\macro Q_DECLARE_TYPEINFO(Type, Flags)
\relates <QtGlobal>
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h
index 64d9f79faf..9b8029990c 100644
--- a/src/corelib/global/qglobal.h
+++ b/src/corelib/global/qglobal.h
@@ -687,6 +687,15 @@ Q_CORE_EXPORT bool qSharedBuild() Q_DECL_NOTHROW;
# define qUtf8Printable(string) QString(string).toUtf8().constData()
#endif
+/*
+ Wrap QString::utf16() with enough casts to allow passing it
+ to QString::asprintf("%ls") without warnings.
+*/
+#ifndef qUtf16Printable
+# define qUtf16Printable(string) \
+ static_cast<const wchar_t*>(static_cast<const void*>(QString(string).utf16()))
+#endif
+
class QString;
Q_CORE_EXPORT QString qt_error_string(int errorCode = -1);
diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp
index 9cc552390e..9556dfbd51 100644
--- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp
@@ -1279,21 +1279,21 @@ void tst_QString::sprintfS()
QCOMPARE(a, QString("foobarwhiz"));
{ // %ls
- QCOMPARE(a.sprintf("%.3ls", QString("Hello").utf16() ), QLatin1String("Hel"));
- QCOMPARE(a.sprintf("%10.3ls", QString("Hello").utf16() ), QLatin1String(" Hel"));
- QCOMPARE(a.sprintf("%.10ls", QString("Hello").utf16() ), QLatin1String("Hello"));
- QCOMPARE(a.sprintf("%10.10ls", QString("Hello").utf16() ), QLatin1String(" Hello"));
- QCOMPARE(a.sprintf("%-10.10ls", QString("Hello").utf16() ), QLatin1String("Hello "));
- QCOMPARE(a.sprintf("%-10.3ls", QString("Hello").utf16() ), QLatin1String("Hel "));
- QCOMPARE(a.sprintf("%-5.5ls", QString("Hello").utf16() ), QLatin1String("Hello"));
+ QCOMPARE(a.sprintf("%.3ls", qUtf16Printable("Hello")), QLatin1String("Hel"));
+ QCOMPARE(a.sprintf("%10.3ls", qUtf16Printable("Hello")), QLatin1String(" Hel"));
+ QCOMPARE(a.sprintf("%.10ls", qUtf16Printable("Hello")), QLatin1String("Hello"));
+ QCOMPARE(a.sprintf("%10.10ls", qUtf16Printable("Hello")), QLatin1String(" Hello"));
+ QCOMPARE(a.sprintf("%-10.10ls", qUtf16Printable("Hello")), QLatin1String("Hello "));
+ QCOMPARE(a.sprintf("%-10.3ls", qUtf16Printable("Hello")), QLatin1String("Hel "));
+ QCOMPARE(a.sprintf("%-5.5ls", qUtf16Printable("Hello")), QLatin1String("Hello"));
// Check utf16 is preserved for %ls
QCOMPARE(a.sprintf("%ls",
- QString::fromUtf8("\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205").utf16()),
+ qUtf16Printable("\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205")),
QLatin1String("\366\344\374\326\304\334\370\346\345\330\306\305"));
int n;
- a.sprintf("%ls%n%s", QString("hello").utf16(), &n, "goodbye");
+ a.sprintf("%ls%n%s", qUtf16Printable("hello"), &n, "goodbye");
QCOMPARE(n, 5);
QCOMPARE(a, QLatin1String("hellogoodbye"));
}