Actually if you are using Qt5, the better approach is QStringLiteral()
. It not only signifies the intent (a compile time constant QString
), but also (slightly) increase efficiency because no runtime conversion from some mulitbyte encoding to UTF-16 is required.
If you need to use Qt4, then conditionally defines it yourself:
#ifndef QStringLiteral
#define QStringLiteral(x) (QString::fromUtf8(x))
#endif
Or as commented by Kuba Ober, add
lessThan(QT_MAJOR_VERSION, 5): DEFINES += QStringLiteral=QString::fromUtf8
to your .pro file.
Quoted from Qt Documentation:
The macro generates the data for a QString out of str at compile time
if the compiler supports it. Creating a QString from it is free in
this case, and the generated string data is stored in the read-only
segment of the compiled object file.
For compilers not supporting the creation of compile time strings,
QStringLiteral will fall back to QString::fromUtf8().
If you have code looking like:
if (node.hasAttribute("http-contents-length")) //...
One temporary QString will be created to be passed as the hasAttribute function
parameter. This can be quite expensive, as it involves a memory
allocation and the copy and the conversion of the data into QString's
internal encoding.
This can be avoided by doing
if (node.hasAttribute(QStringLiteral("http-contents-length"))) //...
Then the QString's internal data will be generated at compile time and
no conversion or allocation will occur at runtime
Using QStringLiteral instead of a double quoted ascii literal can
significantly speed up creation of QString's from data known at
compile time.
If the compiler is C++11 enabled the string str can actually contain
unicode data.