IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QLatin1String Class

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

All functions in this class are reentrant.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QLatin1String Class

  • Header: QLatin1String

  • qmake: QT += core

  • Group: QLatin1String is part of string-processing

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,

 
Sélectionnez
if (str == "auto" || str == "extern"
        || str == "static" || str == "register") {
    ...
}

is much faster than

 
Sélectionnez
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

 
Sélectionnez
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:

 
Sélectionnez
QLabel *label = new QLabel(QLatin1String("MOD"), this);

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

Member Type Documentation

 

[alias, since 5.10] QLatin1String::const_iterator

This typedef provides an STL-style const iterator for QLatin1String.

This typedef was introduced in Qt 5.10.

See Also

[alias, since 5.11] QLatin1String::const_reference

Alias for reference. Provided for compatibility with the STL.

This typedef was introduced in Qt 5.11.

[alias, since 5.10] QLatin1String::const_reverse_iterator

This typedef provides an STL-style const reverse iterator for QLatin1String.

This typedef was introduced in Qt 5.10.

See Also

[alias, since 5.10] QLatin1String::difference_type

Alias for int. Provided for compatibility with the STL.

This typedef was introduced in Qt 5.10.

[alias, since 5.10] QLatin1String::iterator

This typedef provides an STL-style const iterator for QLatin1String.

QLatin1String does not support mutable iterators, so this is the same as const_iterator.

This typedef was introduced in Qt 5.10.

See Also

[alias, since 5.10] QLatin1String::reference

Alias for value_type &. Provided for compatibility with the STL.

This typedef was introduced in Qt 5.10.

[alias, since 5.10] QLatin1String::reverse_iterator

This typedef provides an STL-style const reverse iterator for QLatin1String.

QLatin1String does not support mutable reverse iterators, so this is the same as const_reverse_iterator.

This typedef was introduced in Qt 5.10.

See Also

[alias, since 5.10] QLatin1String::size_type

Alias for int. Provided for compatibility with the STL.

This typedef was introduced in Qt 5.10.

[alias, since 5.10] QLatin1String::value_type

Alias for const char. Provided for compatibility with the STL.

This typedef was introduced in Qt 5.10.

Member Function Documentation

 

[since 5.10] bool QLatin1String::startsWith(QChar ch) const

[since 5.10] bool QLatin1String::startsWith(QChar ch, Qt::CaseSensitivity cs) const

[since 5.10] bool QLatin1String::startsWith(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[since 5.10] bool QLatin1String::startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns true if this Latin-1 string starts with string-view str, Latin-1 string l1, or character ch, respectively; otherwise returns false.

If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.

This function was introduced in Qt 5.10.

See Also

See also endsWith()

[since 5.10] bool QLatin1String::endsWith(QChar ch) const

[since 5.10] bool QLatin1String::endsWith(QChar ch, Qt::CaseSensitivity cs) const

[since 5.10] bool QLatin1String::endsWith(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[since 5.10] bool QLatin1String::endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns true if this Latin-1 string ends with string-view str, Latin-1 string l1, or character ch, respectively; otherwise returns false.

If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.

This function was introduced in Qt 5.10.

See Also

See also startsWith()

[since 5.6] QLatin1String::QLatin1String()

Constructs a QLatin1String object that stores a nullptr.

This function was introduced in Qt 5.6.

QLatin1String::QLatin1String(const char *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

See also latin1()

[since 5.10] QLatin1String::QLatin1String(const char *first, const char *last)

Constructs a QLatin1String object that stores first with length (last - first).

The range [first,last) must remain valid for the lifetime of this Latin-1 string object.

Passing nullptr as first is safe if last is nullptr, too, and results in a null Latin-1 string.

The behavior is undefined if last precedes first, first is nullptr and last is not, or if last - first > INT_MAX.

This function was introduced in Qt 5.10.

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

Constructs a QLatin1String object that stores str with size.

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

See also latin1()

[explicit] 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

See also latin1()

[since 5.8] QLatin1Char QLatin1String::at(int pos) const

Returns the character at position pos in this object.

This function performs no error checking. The behavior is undefined when pos < 0 or pos >= size().

This function was introduced in Qt 5.8.

See Also

See also operator[]()

[since 5.10] QLatin1Char QLatin1String::back() const

Returns the last character in the string. Same as at(size() - 1).

This function is provided for STL compatibility.

Calling this function on an empty string constitutes undefined behavior.

This function was introduced in Qt 5.10.

See Also

See also front(), at(), operator[]()

[since 5.10] QLatin1String::const_iterator QLatin1String::begin() const

Returns a const STL-style iterator pointing to the first character in the string.

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also end(), cbegin(), rbegin(), data()

[since 5.10] QLatin1String::const_iterator QLatin1String::cbegin() const

Same as begin().

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also cend(), begin(), crbegin(), data()

[since 5.10] QLatin1String::const_iterator QLatin1String::cend() const

Same as end().

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also cbegin(), end(), crend()

[since 5.10] void QLatin1String::chop(int length)

Truncates this string by length characters.

Same as *this = left(size() - length).

The behavior is undefined when length < 0 or length > size().

This function was introduced in Qt 5.10.

See Also

See also mid(), left(), right(), chopped(), truncate()

[since 5.10] QLatin1String QLatin1String::chopped(int length) const

Returns the substring of length size() - length starting at the beginning of this object.

Same as left(size() - length).

The behavior is undefined when length < 0 or length > size().

This function was introduced in Qt 5.10.

See Also

See also mid(), left(), right(), chop(), truncate()

[since 5.10] QLatin1String::const_reverse_iterator QLatin1String::crbegin() const

Same as rbegin().

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also crend(), rbegin(), cbegin()

[since 5.10] QLatin1String::const_reverse_iterator QLatin1String::crend() const

Same as rend().

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also crbegin(), rend(), cend()

const char *QLatin1String::data() const

Returns the Latin-1 string stored in this object.

[since 5.10] QLatin1String::const_iterator QLatin1String::end() const

Returns a const STL-style iterator pointing to the imaginary character after the last character in the list.

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also begin(), cend(), rend()

[since 5.10] QLatin1Char QLatin1String::front() const

Returns the first character in the string. Same as at(0).

This function is provided for STL compatibility.

Calling this function on an empty string constitutes undefined behavior.

This function was introduced in Qt 5.10.

See Also

See also back(), at(), operator[]()

[since 5.10] bool QLatin1String::isEmpty() const

Returns whether the Latin-1 string stored in this object is empty (size() == 0) or not.

This function was introduced in Qt 5.10.

See Also

See also isNull(), size()

[since 5.10] bool QLatin1String::isNull() const

Returns whether the Latin-1 string stored in this object is null (data() == nullptr) or not.

This function was introduced in Qt 5.10.

See Also

See also isEmpty(), data()

const char *QLatin1String::latin1() const

Returns the Latin-1 string stored in this object.

[since 5.8] QLatin1String QLatin1String::left(int length) const

Returns the substring of length length starting at position 0 in this object.

This function performs no error checking. The behavior is undefined when length < 0 or length > size().

This function was introduced in Qt 5.8.

See Also

See also mid(), right(), chopped(), chop(), truncate()

[since 5.8] QLatin1String QLatin1String::mid(int start) const

Returns the substring starting at position start in this object, and extending to the end of the string.

This function performs no error checking. The behavior is undefined when start < 0 or start > size().

This function was introduced in Qt 5.8.

See Also

See also left(), right(), chopped(), chop(), truncate()

[since 5.8] QLatin1String QLatin1String::mid(int start, int length) const

This is an overloaded function.

Returns the substring of length length starting at position start in this object.

This function performs no error checking. The behavior is undefined when start < 0, length < 0, or start + length > size().

This function was introduced in Qt 5.8.

See Also

See also left(), right(), chopped(), chop(), truncate()

[since 5.10] QLatin1String::const_reverse_iterator QLatin1String::rbegin() const

Returns a const STL-style reverse iterator pointing to the first character in the string, in reverse order.

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also rend(), crbegin(), begin()

[since 5.10] QLatin1String::const_reverse_iterator QLatin1String::rend() const

Returns a STL-style reverse iterator pointing to one past the last character in the string, in reverse order.

This function is provided for STL compatibility.

This function was introduced in Qt 5.10.

See Also

See also rbegin(), crend(), end()

[since 5.8] QLatin1String QLatin1String::right(int length) const

This function performs no error checking. The behavior is undefined when length < 0 or length > size().

This function was introduced in Qt 5.8.

See Also

See also mid(), left(), chopped(), chop(), truncate()

int QLatin1String::size() const

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

[since 5.10] QLatin1String QLatin1String::trimmed() const

Strips leading and trailing whitespace and returns the result.

Whitespace means any character for which QChar::isSpace() returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

This function was introduced in Qt 5.10.

[since 5.10] void QLatin1String::truncate(int length)

Truncates this string to length length.

Same as *this = left(length).

The behavior is undefined when length < 0 or length > size().

This function was introduced in Qt 5.10.

See Also

See also mid(), left(), right(), chopped(), chop()

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().

[since 4.3] 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.

See Also

[since 5.0] 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.

See Also

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.

[since 4.3] 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.

See Also

[since 5.0] 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.

See Also

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().

[since 4.3] 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.

See Also

[since 5.0] 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.

See Also

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().

[since 4.3] 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.

See Also

[since 5.0] 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.

See Also

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().

[since 4.3] 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.

See Also

[since 5.0] 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.

See Also

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().

[since 4.3] 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.

See Also

[since 5.0] 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.

See Also

[since 5.8] QLatin1Char QLatin1String::operator[](int pos) const

Returns the character at position pos in this object.

This function performs no error checking. The behavior is undefined when pos < 0 or pos >= size().

This function was introduced in Qt 5.8.

See Also

See also at()

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.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+