QLatin1String Class
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal. More...
| Header: | #include <QLatin1String> |
| qmake: | QT += core |
Note: All functions in this class are reentrant.
Public Types
| (alias) | const_iterator |
| (alias) | const_reference |
| (alias) | const_reverse_iterator |
| (alias) | difference_type |
| (alias) | iterator |
| (alias) | reference |
| (alias) | reverse_iterator |
| (alias) | size_type |
| (alias) | value_type |
Detailed Description
Many of QString's member functions are overloaded to accept const char * instead of QString. This includes the copy constructor, the assignment operator, the comparison operators, and various other functions such as insert(), replace(), and indexOf(). These functions are usually optimized to avoid constructing a QString object for the const char * data. For example, assuming str is a QString,
if (str == "auto" || str == "extern" || str == "static" || str == "register") { ... }
is much faster than
if (str == QString("auto") || str == QString("extern") || str == QString("static") || str == QString("register")) { ... }
because it doesn't construct four temporary QString objects and make a deep copy of the character data.
Applications that define QT_NO_CAST_FROM_ASCII (as explained in the QString documentation) don't have access to QString's const char * API. To provide an efficient way of specifying constant Latin-1 strings, Qt provides the QLatin1String, which is just a very thin wrapper around a const char *. Using QLatin1String, the example code above becomes
if (str == QLatin1String("auto") || str == QLatin1String("extern") || str == QLatin1String("static") || str == QLatin1String("register") { ... }
This is a bit longer to type, but it provides exactly the same benefits as the first version of the code, and is faster than converting the Latin-1 strings using QString::fromLatin1().
Thanks to the QString(QLatin1String) constructor, QLatin1String can be used everywhere a QString is expected. For example:
QLabel *label = new QLabel(QLatin1String("MOD"), this);
Note: If the function you're calling with a QLatin1String argument isn't actually overloaded to take QLatin1String, the implicit conversion to QString will trigger a memory allocation, which is usually what you want to avoid by using QLatin1String in the first place. In those cases, using QStringLiteral may be the better option.
See also QString, QLatin1Char, QStringLiteral, and QT_NO_CAST_FROM_ASCII.
Member Type Documentation
[alias] QLatin1String::const_iterator
This is a type alias for iterator.
This alias was introduced in Qt 5.10.
See also iterator and const_reverse_iterator.
[alias] QLatin1String::const_reference
This is a type alias for reference.
Alias for reference. Provided for compatibility with the STL.
This alias was introduced in Qt 5.11.
[alias] QLatin1String::const_reverse_iterator
This is a type alias for reverse_iterator.
This alias was introduced in Qt 5.10.
See also reverse_iterator and const_iterator.
[alias] QLatin1String::difference_type
This is a type alias for int.
Alias for int. Provided for compatibility with the STL.
This alias was introduced in Qt 5.10.
[alias] QLatin1String::iterator
This is a type alias for value_type*.
QLatin1String does not support mutable iterators, so this is the same as const_iterator.
This alias was introduced in Qt 5.10.
See also const_iterator and reverse_iterator.
[alias] QLatin1String::reference
This is a type alias for value_type&.
Alias for value_type &. Provided for compatibility with the STL.
This alias was introduced in Qt 5.10.
[alias] QLatin1String::reverse_iterator
This is a type alias for std::reverse_iterator<iterator>.
QLatin1String does not support mutable reverse iterators, so this is the same as const_reverse_iterator.
This alias was introduced in Qt 5.10.
See also const_reverse_iterator and iterator.
[alias] QLatin1String::size_type
This is a type alias for int.
Alias for int. Provided for compatibility with the STL.
This alias was introduced in Qt 5.10.
[alias] QLatin1String::value_type
This is a type alias for const char.
Alias for const char. Provided for compatibility with the STL.
This alias was introduced in Qt 5.10.