diff options
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp | 4 | ||||
-rw-r--r-- | src/corelib/global/qglobal.cpp | 32 | ||||
-rw-r--r-- | src/corelib/global/qglobal.h | 4 |
3 files changed, 29 insertions, 11 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 b907c9acb1..5befebc982 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -305,7 +305,7 @@ void load(const QString &fileName) { QFile file(fileName); if (!file.exists()) - qCritical("File '%s' does not exist!", qPrintable(fileName)); + qCritical("File '%s' does not exist!", qUtf8Printable(fileName)); } //! [28] @@ -424,7 +424,7 @@ void TheClass::addLabels() //! [37] -qWarning("%s: %s", qPrintable(key), qPrintable(value)); +qWarning("%s: %s", qUtf8Printable(key), qUtf8Printable(value)); //! [37] diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index fb95a1966f..98460481ca 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -580,8 +580,8 @@ Q_STATIC_ASSERT_X(UCHAR_MAX == 255, "Qt assumes that char is 8 bits"); 64-bit integer literals in a platform-independent way. The Q_CHECK_PTR() macro prints a warning containing the source code's file name and line number, saying that the program ran out of - memory, if the pointer is 0. The qPrintable() macro represent an - easy way of printing text. + memory, if the pointer is 0. The qPrintable() and qUtf8Printable() + macros represent an easy way of printing text. Finally, the QT_POINTER_SIZE macro expands to the size of a pointer in bytes, and the QT_VERSION and QT_VERSION_STR macros @@ -3356,17 +3356,31 @@ int qrand() qPrintable() is used. This is because the array returned by QString::toLocal8Bit() will fall out of scope. + \note qDebug(), qWarning(), qCritical(), qFatal() expect %s + arguments to be UTF-8 encoded, while qPrintable() converts to + local 8-bit encoding. Therefore qUtf8Printable() should be used + for logging strings instead of qPrintable(). + + \sa qUtf8Printable() +*/ + +/*! + \macro const char *qUtf8Printable(const QString &str) + \relates <QtGlobal> + \since 5.4 + + Returns \a str as a \c{const char *}. This is equivalent to + \a{str}.toUtf8().constData(). + + The char pointer will be invalid after the statement in which + qUtf8Printable() is used. This is because the array returned by + QString::toUtf8() will fall out of scope. + Example: \snippet code/src_corelib_global_qglobal.cpp 37 - \note qDebug(), qWarning(), qCritical(), qFatal() expect %s - arguments to be UTF-8 encoded, while qPrintable() converts to - local 8-bit encoding. Therefore using qPrintable for logging - strings is only safe if the argument contains only ASCII - characters. - - \sa qDebug(), qWarning(), qCritical(), qFatal() + \sa qPrintable(), qDebug(), qWarning(), qCritical(), qFatal() */ /*! diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index a9621e744c..80c9992e25 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -645,6 +645,10 @@ inline void qUnused(T &x) { (void)x; } # define qPrintable(string) QString(string).toLocal8Bit().constData() #endif +#ifndef qUtf8Printable +# define qUtf8Printable(string) QString(string).toUtf8().constData() +#endif + class QString; Q_CORE_EXPORT QString qt_error_string(int errorCode = -1); |