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 Functions

QLatin1String(const char * str)
QLatin1String(const char * str, int size)
QLatin1String(const QByteArray & str)
const char * data() const
const char * latin1() const
int size() const
bool operator!=(const QString & other) const
bool operator!=(const char * other) const
bool operator!=(const QByteArray & other) const
bool operator<(const QString & other) const
bool operator<(const char * other) const
bool operator<(const QByteArray & other) const
bool operator<=(const QString & other) const
bool operator<=(const char * other) const
bool operator<=(const QByteArray & other) const
bool operator==(const QString & other) const
bool operator==(const char * other) const
bool operator==(const QByteArray & other) const
bool operator>(const QString & other) const
bool operator>(const char * other) const
bool operator>(const QByteArray & other) const
bool operator>=(const QString & other) const
bool operator>=(const char * other) const
bool operator>=(const QByteArray & other) const
bool operator!=(QLatin1String s1, QLatin1String s2)
bool operator<(QLatin1String s1, QLatin1String s2)
bool operator<=(QLatin1String s1, QLatin1String s2)
bool operator==(QLatin1String s1, QLatin1String s2)
bool operator>(QLatin1String s1, QLatin1String s2)
bool operator>=(QLatin1String s1, QLatin1String s2)

Detailed Description

The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.

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);

See also QString, QLatin1Char, and QStringLiteral.

Member Function Documentation

QLatin1String::​QLatin1String(const char * str)

Constructs a QLatin1String object that stores str. Note that if str is 0, an empty string is created; this case is handled by QString.

The string data is not copied. The caller must be able to guarantee that str will not be deleted or modified as long as the QLatin1String object exists.

See also latin1().

QLatin1String::​QLatin1String(const char * str, int size)

Constructs a QLatin1String object that stores str with size. Note that if str is 0, an empty string is created; this case is handled by QString.

The string data is not copied. The caller must be able to guarantee that str will not be deleted or modified as long as the QLatin1String object exists.

See also latin1().

QLatin1String::​QLatin1String(const QByteArray & str)

Constructs a QLatin1String object that stores str.

The string data is not copied. The caller must be able to guarantee that str will not be deleted or modified as long as the QLatin1String object exists.

See also latin1().

const char * QLatin1String::​data() const

Returns the Latin-1 string stored in this object.

const char * QLatin1String::​latin1() const

Returns the Latin-1 string stored in this object.

int QLatin1String::​size() const

Returns the size of the Latin-1 string stored in this object.

bool QLatin1String::​operator!=(const QString & other) const

Returns true if this string is not equal to string other; otherwise returns false.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with QString::localeAwareCompare().

bool QLatin1String::​operator!=(const char * other) const

This function overloads operator!=().

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 4.3.

bool QLatin1String::​operator!=(const QByteArray & other) const

This function overloads operator!=().

The other byte array is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 5.0.

bool QLatin1String::​operator<(const QString & other) const

Returns true if this string is lexically less than the other string; otherwise returns false.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings using the QString::localeAwareCompare() function.

bool QLatin1String::​operator<(const char * other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 4.3.

bool QLatin1String::​operator<(const QByteArray & other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 5.0.

bool QLatin1String::​operator<=(const QString & other) const

Returns true if this string is lexically less than or equal to string other; otherwise returns false.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with QString::localeAwareCompare().

bool QLatin1String::​operator<=(const char * other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 4.3.

bool QLatin1String::​operator<=(const QByteArray & other) const

This is an overloaded function.

The other array is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 5.0.

bool QLatin1String::​operator==(const QString & other) const

Returns true if this string is equal to string other; otherwise returns false.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with QString::localeAwareCompare().

bool QLatin1String::​operator==(const char * other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 4.3.

bool QLatin1String::​operator==(const QByteArray & other) const

This is an overloaded function.

The other byte array is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 5.0.

bool QLatin1String::​operator>(const QString & other) const

Returns true if this string is lexically greater than string other; otherwise returns false.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with QString::localeAwareCompare().

bool QLatin1String::​operator>(const char * other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 4.3.

bool QLatin1String::​operator>(const QByteArray & other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 5.0.

bool QLatin1String::​operator>=(const QString & other) const

Returns true if this string is lexically greater than or equal to string other; otherwise returns false.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with QString::localeAwareCompare().

bool QLatin1String::​operator>=(const char * other) const

This is an overloaded function.

The other const char pointer is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 4.3.

bool QLatin1String::​operator>=(const QByteArray & other) const

This is an overloaded function.

The other array is converted to a QString using the QString::fromUtf8() function.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

This function was introduced in Qt 5.0.

Related Non-Members

bool operator!=(QLatin1String s1, QLatin1String s2)

Returns true if string s1 is lexically unequal to string s2; otherwise returns false.

bool operator<(QLatin1String s1, QLatin1String s2)

Returns true if string s1 is lexically smaller than string s2; otherwise returns false.

bool operator<=(QLatin1String s1, QLatin1String s2)

Returns true if string s1 is lexically smaller than or equal to string s2; otherwise returns false.

bool operator==(QLatin1String s1, QLatin1String s2)

Returns true if string s1 is lexically equal to string s2; otherwise returns false.

bool operator>(QLatin1String s1, QLatin1String s2)

Returns true if string s1 is lexically greater than string s2; otherwise returns false.

bool operator>=(QLatin1String s1, QLatin1String s2)

Returns true if string s1 is lexically greater than or equal to string s2; otherwise returns false.

© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.