QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.
QByteArray可用于存储原始字节(包括‘\0’s)和传统的以8位‘\0’s结尾的字符串。使用QByteArray比使用const char *要方便得多。在幕后,它总是确保数据后面有一个“\0”终止符,并使用隐式共享(写时复制)来减少内存使用,避免不必要的数据复制。
In addition to QByteArray, Qt also provides the QString class to store string data. For most purposes, QString is the class you want to use. It stores 16-bit Unicode characters, making it easy to store non-ASCII/non-Latin-1 characters in your application. Furthermore, QString is used throughout in the Qt API. The two main cases where QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (e.g., with Qt for Embedded Linux).
One way to initialize a QByteArray is simply to pass a const char * to its constructor. For example, the following code creates a byte array of size 5 containing the data "Hello":
除了QByteArray,Qt还提供了QString类来存储字符串数据。对于大多数目的,QString是您想要使用的类。它存储16位的Unicode字符,便于在应用程序中存储non-ASCII/non-Latin-1字符。此外,QString在Qt API中贯穿始终地使用。QByteArray最合适的两种主要情况是,当您需要存储原始二进制数据和内存保存很关键时(例如,对于嵌入式Linux的Qt)。 初始化QByteArray的一种方法是将const字符*传递给它的构造函数。例如,下面的代码创建了一个大小为5的字节数组,其中包含数据“Hello”:
QByteArray ba("Hello");
Although the size() is 5, the byte array also maintains an extra '\0' character at the end so that if a function is used that asks for a pointer to the underlying data (e.g. a call to data()), the data pointed to is guaranteed to be '\0'-terminated.
虽然size()为5,但字节数组在结尾还保留一个额外的“\0”字符,以便如果使用函数请求指向底层数据的指针(例如对data()的调用),则指向的数据保证以“\0”终止。
QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects. (If for performance reasons you don't want to take a deep copy of the character data, use QByteArray::fromRawData() instead.)
QByteArray是const char *数据的深度副本,因此您可以在以后修改它,而不会产生副作用。(如果出于性能原因不想深度复制字符数据,请使用 QByteArray::fromRawData()
Another approach is to set the size of the array using resize() and to initialize the data byte per byte. QByteArray uses 0-based indexes, just like C++ arrays. To access the byte at a particular index position, you can use operator[](). On non-const byte arrays, operator[]() returns a reference to a byte that can be used on the left side of an assignment. For example:
另一种方法是使用调整resize()来设置数组的大小,并初始化每个字节的数据字节。QByteArray使用基于0的索引,就像C++数组一样。要访问特定索引位置上的字节,可以使用 operator[]()。在非常量字节数组上, operator[]()返回一个对可以在赋值左侧使用的字节的引用。例如:
QByteArray ba;
ba.resize(5);
ba[0] = 0x3c;
ba[1] = 0xb8;
ba[2] = 0x64;
ba[3] = 0x18;
ba[4] = 0xca;
For read-only access, an alternative syntax is to use at():
对于只读访问,现在,您还可以使用另一种语法at():
for (int i = 0; i < ba.size(); ++i) {
if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
cout << "Found character in range [a-f]" << Qt::endl;
}
可以比操作符[]()更快,因为它永远不会导致深度复制发生。
at() can be faster than operator[](), because it never causes a deep copy to occur.
To extract many bytes at a time, use left(), right(), or mid().
要一次提取多个字节,请使用left()、right()或mid()。
A QByteArray can embed '\0' bytes. The size() function always returns the size of the whole array, including embedded '\0' bytes, but excluding the terminating '\0' added by QByteArray. For example:
QByteArray可以嵌入“\0”字节。size()函数总是返回整个数组的大小,包括嵌入的“\0”字节,但不包括由QByteArray添加的终止的“\0”。例如:
QByteArray ba1("ca\0r\0t");
ba1.size(); // Returns 2.
ba1.constData(); // Returns "ca" with terminating \0.
QByteArray ba2("ca\0r\0t", 3);
ba2.size(); // Returns 3.
ba2.constData(); // Returns "ca\0" with terminating \0.
QByteArray ba3("ca\0r\0t", 4);
ba3.size(); // Returns 4.
ba3.constData(); // Returns "ca\0r" with terminating \0.
const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
QByteArray ba4(QByteArray::fromRawData(cart, 6));
ba4.size(); // Returns 6.
ba4.constData(); // Returns "ca\0r\0t" without terminating \0.
If you want to obtain the length of the data up to and excluding the first '\0' character, call qstrlen() on the byte array.
如果要获得最不包含第一个“\0”字符的数据长度,请在字节数组上调用qstrlen()。
After a call to resize(), newly allocated bytes have undefined values. To set all the bytes to a particular value, call fill().
在调用调整()大小后,新分配的字节有未定义的值。要将所有字节设置为一个特定的值,请调用fill()。
To obtain a pointer to the actual character data, call data() or constData(). These functions return a pointer to the beginning of the data. The pointer is guaranteed to remain valid until a non-const function is called on the QByteArray. It is also guaranteed that the data ends with a '\0' byte unless the QByteArray was created from a raw data. This '\0' byte is automatically provided by QByteArray and is not counted in size().
要获得指向实际字符数据的指针,请调用data()或constData()。这些函数返回一个指向数据开头的指针。在QByteArray上调用非常量函数之前,保证指针保持有效。还可以保证数据以“\0”字节结束,除非QByteArray是从原始数据创建的。这个“\0”字节由QByteArray自动提供,其大小不计算为size()。
QByteArray provides the following basic functions for modifying the byte data: append(), prepend(), insert(), replace(), and remove(). For example:
QByteArray提供了以下修改字节数据的基本函数:append(), prepend(), insert(), replace(), and remove()
QByteArray x("and");
x.prepend("rock "); // x == "rock and"
x.append(" roll"); // x == "rock and roll"
x.replace(5, 3, "&"); // x == "rock & roll"
The replace() and remove() functions' first two arguments are the position from which to start erasing and the number of bytes that should be erased.
replace() and remove()函数的前两个参数是开始擦除的位置和应该擦除的字节数。
When you append() data to a non-empty array, the array will be reallocated and the new data copied to it. You can avoid this behavior by calling reserve(), which preallocates a certain amount of memory. You can also call capacity() to find out how much memory QByteArray actually allocated. Data appended to an empty array is not copied.
当您append()数据附加到非空数组时,该数组将被重新分配,并将新数据复制到其中。您可以通过调用reserve()来避免这种行为,它预先分配了一定量的内存。您还可以调用capacity()来了解实际分配的内存QByteArray。不复制附加到空数组中的数据。
A frequent requirement is to remove whitespace characters from a byte array ('\n', '\t', ' ', etc.). If you want to remove whitespace from both ends of a QByteArray, use trimmed(). If you want to remove whitespace from both ends and replace multiple consecutive whitespaces with a single space character within the byte array, use simplified().
一个常见的要求是从字节数组中删除空白字符(“\n”、“\t”、“”等)。如果要从QByteArray的两端删除空格,请使用trimmed()。如果要从两端删除空格,并在字节数组中用一个空格字符替换多个连续的空格,请使用simplified()。
If you want to find all occurrences of a particular character or substring in a QByteArray, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index position of the character or substring if they find it; otherwise, they return -1. For example, here's a typical loop that finds all occurrences of a particular substring:
如果您想在q字节array中找到特定字符或子字符串的所有出现,请使用indexOf()或lastIndexOf()。前者从给定的索引位置开始向前搜索,后者向后搜索。如果找到字符或子字符串,两者都返回其索引位置;否则,它们返回-1。例如,这里有一个典型的循环,它查找特定子字符串的所有出现:
QByteArray ba("We must be bold, very bold");
int j = 0;
while ((j = ba.indexOf("", j)) != -1) {
cout << "Found tag at index position " << j << Qt::endl;
++j;
}
If you simply want to check whether a QByteArray contains a particular character or substring, use contains(). If you want to find out how many times a particular character or substring occurs in the byte array, use count(). If you want to replace all occurrences of a particular value with another, use one of the two-parameter replace() overloads.
如果您只是想检查QByteArray是否包含特定的字符或子字符串,请使用contains()。如果您想知道一个特定字符或子字符串在字节数组中出现的次数,请使用count()。如果要用某个特定值替换出现的所有值,请使用双参数replace过载之一。
QByteArrays can be compared using overloaded operators such as operator<(), operator<=(), operator==(), operator>=(), and so on. The comparison is based exclusively on the numeric values of the characters and is very fast, but is not what a human would expect. QString::localeAwareCompare() is a better choice for sorting user-interface strings.
QByteArrays 可以使用重载操作符进行比较,如操作符<()、操作符<=()、操作符==()、操作符>=()等。他的比较完全基于字符的数值,而且非常快,但这并不是人们所期望的。QString::localeAwareCompare() 是对用户界面字符串进行排序的一个更好的选择。
For historical reasons, QByteArray distinguishes between a null byte array and an empty byte array. A null byte array is a byte array that is initialized using QByteArray's default constructor or by passing (const char *)0 to the constructor. An empty byte array is any byte array with size 0. A null byte array is always empty, but an empty byte array isn't necessarily null:
由于历史原因,QByteArray可以区分null 字节数组和empty 字节数组。null 字节数组是使用QByteArray的默认构造函数或通过将(const char *)0传递给构造函数来初始化的字节数组。empty 数组是大小为0的任何字节数组。空字节数组总是空的,但empty 字节数组不一定为空:
QByteArray().isNull(); // returns true
QByteArray().isEmpty(); // returns true
QByteArray("").isNull(); // returns false
QByteArray("").isEmpty(); // returns true
QByteArray("abc").isNull(); // returns false
QByteArray("abc").isEmpty(); // returns false
All functions except isNull() treat null byte arrays the same as empty byte arrays. For example, data() returns a valid pointer (not nullptr) to a '\0' character for a byte array and QByteArray() compares equal to QByteArray(""). We recommend that you always use isEmpty() and avoid isNull().
除isNull()外,所有函数都将null 字节数组与empty 字节数组相同。例如,data()为一个字节数组返回一个指向‘\0’字符的有效指针(不是nullptr),并且QByteArray()比较等于QByteArray("")。建议始终使用isEmpty() ),避免isNull()。
The current version of QByteArray is limited to just under 2 GB (2^31 bytes) in size. The exact value is architecture-dependent, since it depends on the overhead required for managing the data block, but is no more than 32 bytes. Raw data blocks are also limited by the use of int type in the current version to 2 GB minus 1 byte.
当前版本的QByteArray的大小被限制在2 GB(2^31字节)以下。确切的值依赖于体系结构,因为它依赖于管理数据块所需的开销,但不超过32个字节。原始数据块也受到当前版本中使用int类型的限制为2GB-1字节
In case memory allocation fails, QByteArray will throw a std::bad_alloc exception. Out of memory conditions in the Qt containers are the only case where Qt will throw exceptions.
如果内存分配失败,QByteArray将抛出一个std::bad_alloc异常。Qt容器中内存不足是Qt抛出异常的唯一情况。
Note that the operating system may impose further limits on applications holding a lot of allocated memory, especially large, contiguous blocks. Such considerations, the configuration of such behavior or any mitigation are outside the scope of the QByteArray API.
注意,操作系统可能会对持有大量分配内存的应用程序施加进一步的限制,特别是大的连续块。这些考虑事项、此类行为的配置或任何缓解措施都超出了QByteArray API的范围。
Number-String Conversions
数字字符串转换
Functions that perform conversions between numeric data types and strings are performed in the C locale, irrespective of the user's locale settings. Use QString to perform locale-aware conversions between numbers and strings. 转换的函数在C区域环境中执行,不管用户的区域设置设置如何。使用QString可以在数字和字符串之间执行对区域设置有感知能力的转换
In QByteArray, the notion of uppercase and lowercase and of which character is greater than or less than another character is done in the Latin-1 locale. This affects functions that support a case insensitive option or that compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only Latin-1 characters.
在QByteArray中,大写和小写以及大于或小于其他字符的概念是在拉丁语-1区域设置中完成的。这将影响支持不区分大小写的选项的函数,或比较或小写或大写其参数的函数。如果两个字符串都只包含Latin-1个字符,则不区分大小写的操作和比较将是准确的。这种影响的功能包括contains(), indexOf(), lastIndexOf(), operator<(), operator<=(), operator>(), operator>=(), isLower(), isUpper(), toLower() and toUpper().
Functions that this affects include contains(), indexOf(), lastIndexOf(), operator<(), operator<=(), operator>(), operator>=(), isLower(), isUpper(), toLower() and toUpper().
This issue does not apply to QStrings since they represent characters using Unicode.
这个问题不适用于QStrings,因为它们使用Unicode表示字符。
See also QString and QBitArray.
另请参见QString和QBitArray。
QString Class
Detailed Description
QString stores a string of 16-bit QChars, where each QChar corresponds to one UTF-16 code unit. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChars.)
QString存储一个16位QChars的字符串,其中每个QChar对应一个UTF-16代码单元。(代码值大于65535的Unicode字符使用代理对存储,即两个连续的QChars。)
Unicode is an international standard that supports most of the writing systems in use today. It is a superset of US-ASCII (ANSI X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1 characters are available at the same code positions.
Unicode是一个国际标准,它支持目前使用的大多数编写系统。它是US-ASCII(ANSI X3.4-1986)和Latin-1(ISO 8859-1)的超集,所有的US-ASCII/Latin-1字符都在相同的代码位置上提供。
Behind the scenes, QString uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data. This also helps reduce the inherent overhead of storing 16-bit characters instead of 8-bit characters.
在幕后,QString使用隐式共享(写时复制)来减少内存使用,并避免不必要的数据复制。这也有助于减少存储16位字符而不是8位字符的固有开销。
In addition to QString, Qt also provides the QByteArray class to store raw bytes and traditional 8-bit '\0'-terminated strings. For most purposes, QString is the class you want to use. It is used throughout the Qt API, and the Unicode support ensures that your applications will be easy to translate if you want to expand your application's market at some point. The two main cases where QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (like in embedded systems).
除了QString之外,Qt还提供了QByteArray类来存储原始字节和传统的终止8位‘\0”的字符串。对于大多数目的,QString是您想要使用的类。它在整个Qt API中使用,并且Unicode支持确保了如果您想扩展应用程序的市场在某个时候,您的应用程序将易于翻译。QByteArray适合的两种主要情况是,当您需要存储原始二进制数据和内存保存非常关键时(比如在嵌入式系统中)。
One way to initialize a QString is simply to pass a const char * to its constructor. For example, the following code creates a QString of size 5 containing the data "Hello":
初始化QString的一种方法是将const char *传递给它的构造函数。例如,下面的代码创建了一个大小为5的QString,其中包含数据“Hello”:
QString str = "Hello";
QString converts the const char * data into Unicode using the fromUtf8() function.
QString使用fromUtf8()函数将char * 数据转换为Unicode。
In all of the QString functions that take const char * parameters, the const char * is interpreted as a classic C-style '\0'-terminated string encoded in UTF-8. It is legal for the const char * parameter to be nullptr.
在所有采用常量char *参数的QString函数中,常量char *被解释为用UTF-8编码的以“\0”结尾的经典c型字符串。常量char *参数为nullptr是合法的。
You can also provide string data as an array of QChars:
您还可以提供字符串数据作为QChars的数组:
static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
QString str(data, 4);
QString makes a deep copy of the QChar data, so you can modify it later without experiencing side effects. (If for performance reasons you don't want to take a deep copy of the character data, use QString::fromRawData() instead.)
QString对QChar数据进行深度复制,因此您可以在以后修改它,而不会产生副作用。(如果出于性能原因,您不想获取字符数据的深度副本,请使用QString::fromRawData())
Another approach is to set the size of the string using resize() and to initialize the data character per character. QString uses 0-based indexes, just like C++ arrays. To access the character at a particular index position, you can use operator[](). On non-const strings, operator[]() returns a reference to a character that can be used on the left side of an assignment. For example:
另一种方法是使用调整大小()来设置字符串的大小,并初始化每个字符的数据字符。QString使用基于0的索引,就像C-++数组一样。要访问特定索引位置的字符,可以使用操作符[]()。在非常量字符串上,运算符[]()返回对可在赋值左侧使用的字符的引用。例如:
QString str;
str.resize(4);
str[0] = QChar('U');
str[1] = QChar('n');
str[2] = QChar(0x10e3);
str[3] = QChar(0x03a3);
For read-only access, an alternative syntax is to use the at() function:
对于只读访问,另一种语法是使用at()函数:
QString str;
for (int i = 0; i < str.size(); ++i) {
if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
qDebug() << "Found character in range [a-f]";
}
The at() function can be faster than operator[](), because it never causes a deep copy to occur. Alternatively, use the left(), right(), or mid() functions to extract several characters at a time.
at()函数可以比操作符[]()更快,因为它不会导致深度复制发生。或者,使用left()、right()或mid()函数一次提取多个字符。
A QString can embed '\0' characters (QChar::Null). The size() function always returns the size of the whole string, including embedded '\0' characters.
QString可以嵌入“\0”字符(QChar::Null)。size()函数总是返回整个字符串的大小,包括嵌入的‘\0’字符。
After a call to the resize() function, newly allocated characters have undefined values. To set all the characters in the string to a particular value, use the fill() function.
在调用了resize()函数后,新分配的字符具有未定义的值。要将字符串中的所有字符设置为一个特定的值,请使用fill()函数。
QString provides dozens of overloads designed to simplify string usage. For example, if you want to compare a QString with a string literal, you can write code like this and it will work as expected:
QString提供了几十个旨在简化字符串使用的过载。例如,如果您想比较一个QString和一个字符串文字,您可以编写这样的代码,它将按预期工作:
QString str;
if (str == "auto" || str == "extern"
|| str == "static" || str == "register") {
// ...
}
You can also pass string literals to functions that take QStrings as arguments, invoking the QString(const char *) constructor. Similarly, you can pass a QString to a function that takes a const char * argument using the qPrintable() macro which returns the given QString as a const char *. This is equivalent to calling
您还可以将字符串文字传递给以QString为参数的函数,从而调用QString(const char *)构造函数。类似地,您可以将一个QString传递给一个函数,该函数使用 const char *参数,使用qPrintable()宏返回给定的QString作为const char *。这相当于调用
QString provides the following basic functions for modifying the character data: append(), prepend(), insert(), replace(), and remove(). For example:
QString提供了以下修改字符数据的基本功能:append(), prepend(), insert(), replace(), and remove(). 例如:
QString str = "and";
str.prepend("rock "); // str == "rock and"
str.append(" roll"); // str == "rock and roll"
str.replace(5, 3, "&"); // str == "rock & roll"
If you are building a QString gradually and know in advance approximately how many characters the QString will contain, you can call reserve(), asking QString to preallocate a certain amount of memory. You can also call capacity() to find out how much memory QString actually allocated.
如果您正在逐步构建一个QString,并且提前知道QString将包含多少字符,您可以调用reserve(),要求QString预先分配一定数量的内存。您还可以调用 capacity()来找出实际分配了多少内存QString。
The replace() and remove() functions' first two arguments are the position from which to start erasing and the number of characters that should be erased. If you want to replace all occurrences of a particular substring with another, use one of the two-parameter replace() overloads.
replace() and remove() 函数的前两个参数是开始擦除的位置和应该擦除的字符数。如果要用另一个子字符串替换所有出现的特定子字符串,请使用双参数replace()重载中的一个。
A frequent requirement is to remove whitespace characters from a string ('\n', '\t', ' ', etc.). If you want to remove whitespace from both ends of a QString, use the trimmed() function. If you want to remove whitespace from both ends and replace multiple consecutive whitespaces with a single space character within the string, use simplified().
一个常见的要求是从字符串中删除空格字符 ('\n', '\t', ' ', etc.),如果您想从QString的两端删除空格,使用trimmed()函数,如果要从两端删除空格,并在字符串中的单个空格字符替换多个连续空格,请使用简化的simplified()。
If you want to find all occurrences of a particular character or substring in a QString, use the indexOf() or lastIndexOf() functions. The former searches forward starting from a given index position, the latter searches backward. Both return the index position of the character or substring if they find it; otherwise, they return -1. For example, here is a typical loop that finds all occurrences of a particular substring:
如果您想在QString中找到特定字符或子字符串的所有出现,请使用indexOf()或 lastIndexOf() 函数。前者从给定的索引位置开始向前搜索,后者向后搜索。如果找到字符或子字符串,两者都返回其索引位置;否则,它们返回-1。
例如,这里是一个典型的循环,它查找特定子字符串的所有出现:
QString str = "We must be bold, very bold";
int j = 0;
while ((j = str.indexOf("", j)) != -1) {
qDebug() << "Found tag at index position" << j;
++j;
}
QString provides many functions for converting numbers into strings and strings into numbers. See the arg() functions, the setNum() functions, the number() static functions, and the toInt(), toDouble(), and similar functions.
QString提供了许多将数字转换为字符串和将字符串转换为数字的函数。参见arg()函数、setNum()函数、number()静态函数以及toInt()、toDouble()和类似的函数。
To get an upper- or lowercase version of a string use toUpper() or toLower().
转换字符串大小写使用toUpper() or toLower().
Lists of strings are handled by the QStringList class. You can split a string into a list of strings using the split() function, and join a list of strings into a single string with an optional separator using QStringList::join(). You can obtain a list of strings from a string list that contain a particular substring or that match a particular QRegExp using the QStringList::filter() function.
字符串列表由QStringList类处理。可以使用split()函数将字符串拆分为字符串列表,也可以使用QStringList::join()使用可选分隔符将字符串列表连接为单个字符串。可以使用QStringList::filter()函数从包含特定子字符串或匹配特定QRegExp的字符串列表中获取字符串列表。
If you want to see if a QString starts or ends with a particular substring use startsWith() or endsWith(). If you simply want to check whether a QString contains a particular character or substring, use the contains() function. If you want to find out how many times a particular character or substring occurs in the string, use count().
如果您想看看一个QString是以一个特定的子字符串开始还是结束使用startsWith() or endsWith(). 如果您只是想检查一个QString是否包含一个特定的字符或子字符串,使用contains() 函数。如果您想找出一个特定的字符或子字符串在该字符串中出现的次数,请使用count().
QStrings can be compared using overloaded operators such as operator<(), operator<=(), operator==(), operator>=(), and so on. Note that the comparison is based exclusively on the numeric Unicode values of the characters. It is very fast, but is not what a human would expect; the QString::localeAwareCompare() function is a better choice for sorting user-interface strings.
QStrings可以使用重载的操作符进行比较,例如operator<(), operator<=(), operator==(), operator>=(),等等请注意,比较完全基于字符的数字Unicode值。它非常快,但并不是人类所期望的; QString::localeAwareCompare() 函数是对用户界面字符串进行排序的一个更好的选择。
To obtain a pointer to the actual character data, call data() or constData(). These functions return a pointer to the beginning of the QChar data. The pointer is guaranteed to remain valid until a non-const function is called on the QString.
若要获取指向实际字符数据的指针,请调用 data() or constData().这些函数返回一个指向QChar数据开头的指针。在QString上调用非常量函数之前,保证指针保持有效。
QString provides the following three functions that return a const char * version of the string as QByteArray: toUtf8(), toLatin1(), and toLocal8Bit().
QString提供了以下三个函数,它们返回的字符串的const char *版本为QByteArray: toUtf8(), toLatin1(), and toLocal8Bit().
To convert from one of these encodings, QString provides fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other encodings are supported through the QTextCodec class.
要从这些编码之一进行转换,QString提供了 fromLatin1(), fromUtf8(), and fromLocal8Bit().其他的编码也通过QTextCodec类得到支持。
As mentioned above, QString provides a lot of functions and operators that make it easy to interoperate with const char * strings. But this functionality is a double-edged sword: It makes QString more convenient to use if all strings are US-ASCII or Latin-1, but there is always the risk that an implicit conversion from or to const char * is done using the wrong 8-bit encoding. To minimize these risks, you can turn off these implicit conversions by defining the following two preprocessor symbols:
如上所述,QString提供了许多函数和操作符,使其易于与const char *字符串进行互操作。但这个功能是一把双刃剑:如果所有字符串都是US-ASCII或Latin-1,那么QString使用起来更方便,但是总是存在使用错误的const char *的隐式转换或使用错误的8位编码的风险。为了最小化这些风险,您可以通过定义以下两个预处理器符号来关闭这些隐式转换:
One way to define these preprocessor symbols globally for your application is to add the following entry to your qmake project file:
QT_NO_CAST_TO_ASCII禁用从QString到C字符串的自动转换。 为应用程序全局定义这些预处理器符号的一种方法是将以下条目添加到qmake项目文件中
DEFINES += QT_NO_CAST_FROM_ASCII \
QT_NO_CAST_TO_ASCII
You then need to explicitly call fromUtf8(), fromLatin1(), or fromLocal8Bit() to construct a QString from an 8-bit string, or use the lightweight QLatin1String class, for example:
然后,您需要显式地调用 fromUtf8(), fromLatin1(), or fromLocal8Bit() 要从一个8位的字符串构造一个QString, 或者使用轻量级QLatin1String 类,例如:
QString url = QLatin1String("http://www.unicode.org/");
Similarly, you must call toLatin1(), toUtf8(), or toLocal8Bit() explicitly to convert the QString to an 8-bit string. (Other encodings are supported through the QTextCodec class.)
同样,你必须调用 toLatin1(), toUtf8(), or toLocal8Bit() 显式地将QString转换为8位字符串。(其他编码通过QTextCodec类得到支持)。
Note for C Programmers |
Due to C++'s type system and the fact that QString is implicitly shared, QStrings may be treated like ints or other basic types. For example: 由于C++的类型系统和QString是隐式共享的事实,QString可以被当作int或其他基本类型来处理。例如: QString Widget::boolToString(bool b) { QString result; if (b) result = "True"; else result = "False"; return result; } The result variable, is a normal variable allocated on the stack. When return is called, and because we're returning by value, the copy constructor is called and a copy of the string is returned. No actual copying takes place thanks to the implicit sharing. 结果变量,是分配在堆栈上的一个普通变量。当调用返回时,因为我们是按值返回的,所以就调用了复制构造函数,并返回了字符串的副本。由于隐式共享,没有发生实际的复制。 |
For historical reasons, QString distinguishes between a null string and an empty string. A null string is a string that is initialized using QString's default constructor or by passing (const char *)0 to the constructor. An empty string is any string with size 0. A null string is always empty, but an empty string isn't necessarily null:
由于历史原因,QString可以区分为空字符串和空字符串。空字符串是使用QString的默认构造函数或通过将(const char *)0传递给构造函数来初始化的字符串。empty字符串是大小为0的任何字符串。null字符串总是空的,但empty 符串不一定是null:
QString().isNull(); // returns true
QString().isEmpty(); // returns true
QString("").isNull(); // returns false
QString("").isEmpty(); // returns true
QString("abc").isNull(); // returns false
QString("abc").isEmpty(); // returns false
All functions except isNull() treat null strings the same as empty strings. For example, toUtf8().constData() returns a valid pointer (not nullptr) to a '\0' character for a null string. We recommend that you always use the isEmpty() function and avoid isNull().
除isNull()之外,所有函数都将null 字符串视为与empty 字符串相同。例如,toUtf8().constData() 返回一个指向空字符串的“\0”字符的有效指针(而不是nullptr)。我们建议您始终使用isEmpty()函数,并避免使用isNull()。
In member functions where an argument format can be specified (e.g., arg(), number()), the argument format can be one of the following:
在可以指定参数格式的成员函数中(例如,arg(),number()),参数格式可以是以下格式之一:
Format |
Meaning |
e |
format as [-]9.9e[+|-]999 |
E |
format as [-]9.9E[+|-]999 |
f |
format as [-]9.9 |
g |
use e or f format, whichever is the most concise 使用e或f格式,以最简洁的格式为准 |
G |
use E or f format, whichever is the most concise 使用E格式或f格式,以最简洁的格式为准 |
A precision is also specified with the argument format. For the 'e', 'E', and 'f' formats, the precision represents the number of digits after the decimal point. For the 'g' and 'G' formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).
还使用参数格式指定了精度。对于“e”、“e”和“f”格式,精度表示小数点后的位数。对于“g”和“g”格式,精度表示重要数字的最大数量(省略了后面的0)。
Many strings are known at compile time. But the trivial constructor QString("Hello"), will copy the contents of the string, treating the contents as Latin-1. To avoid this one can use the QStringLiteral macro to directly create the required data at compile time. Constructing a QString out of the literal does then not cause any overhead at runtime.许多字符串在编译时都是已知的。但是简单的构造函数QString(“Hello”)将复制字符串的内容,并将这些内容视为Latin-1。为了避免这种情况,可以使用QStringLiteral 宏在编译时直接创建所需的数据。从文字中构造一个QString在运行时不会造成任何开销。
A slightly less efficient way is to use QLatin1String. This class wraps a C string literal, precalculates it length at compile time and can then be used for faster comparison with QStrings and conversion to QStrings than a regular C string literal.一种效率稍低的方法是使用QLatin1String。这个类包装一个C字符串文字,在编译时预计算它的长度,然后可以用于与常规C字符串字符更快地比较和转换到QStrings。
Using the QString '+' operator, it is easy to construct a complex string from multiple substrings. You will often write code like this:
使用QString ‘+’操作符,很容易从多个子字符串构造一个复杂的字符串。您会经常编写这样的代码:
QString foo;
QString type = "long";
foo->setText(QLatin1String("vector<") + type + QLatin1String(">::iterator"));
if (foo.startsWith("(" + type + ") 0x"))
...
There is nothing wrong with either of these string constructions, but there are a few hidden inefficiencies. Beginning with Qt 4.6, you can eliminate them.
这两种字符串结构都没有任何问题,但也有一些隐藏的低效问题。从Qt 4.6开始,您可以消除它们。
First, multiple uses of the '+' operator usually means multiple memory allocations. When concatenating n substrings, where n > 2, there can be as many as n - 1 calls to the memory allocator.
首先,“+”操作符的多次使用通常意味着多个内存分配。当连接n个子字符串时,其中n个>2,可以有多达n - 1个调用内存分配器。
In 4.6, an internal template class QStringBuilder has been added along with a few helper functions. This class is marked internal and does not appear in the documentation, because you aren't meant to instantiate it in your code. Its use will be automatic, as described below. The class is found in src/corelib/tools/qstringbuilder.cpp if you want to have a look at it.
在4.6中,添加了一个内部模板类QStringBuilder和一些辅助函数。这个类被标记为内部类,并且不会出现在文档中,因为您并不打算在代码中实例化它。其使用将是自动的,如下所述。该类可以在src/corelib/tools/qstringbuilder.cpp如果你想看一下它。
QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.
QStringBuilder使用表达式模板并重新实现“%”操作符,以便当您使用“%”作为字符串连接而不是“+”时,多个子字符串连接将被推迟,直到最终结果即将分配给QString。此时,最终结果所需的内存量是已知的。然后调用一次内存分配器以获得所需的空间,并将子字符串逐个复制到它中。
Additional efficiency is gained by inlining and reduced reference counting (the QString created from a QStringBuilder typically has a ref count of 1, whereas QString::append() needs an extra test).
通过内联和减少引用计数(从一个QStringBuilder 通常它的参考计数为1,而QString::append() 需要一个额外的测试)。
There are two ways you can access this improved method of string construction. The straightforward way is to include QStringBuilder wherever you want to use it, and use the '%' operator instead of '+' when concatenating strings:
有两种方法可以访问这种改进的字符串构造方法。简单的方法是在你想使用它的地方包含qstr,并在连接字符串时使用“%”运算符而不是“+”:
#include
QString hello("hello");
QStringRef el(&hello, 2, 3);
QLatin1String world("world");
QString message = hello % el % world % QChar('!');
A more global approach which is the most convenient but not entirely source compatible, is to this define in your .pro file:
一个更全局的方法是最方便但不完全兼容源代码,是在你的。pro文件中定义:
DEFINES *= QT_USE_QSTRINGBUILDER
and the '+' will automatically be performed as the QStringBuilder '%' everywhere.
而“+”将自动被执行为QStringBuilder “%”。
The current version of QString is limited to just under 2 GB (2^31 bytes) in size. The exact value is architecture-dependent, since it depends on the overhead required for managing the data block, but is no more than 32 bytes. Raw data blocks are also limited by the use of int type in the current version to 2 GB minus 1 byte. Since QString uses two bytes per character, that translates to just under 2^30 characters in one QString.
当前版本的QString的大小被限制在2 GB(2^31字节)以下。确切的值依赖于体系结构,因为它依赖于管理数据块所需的开销,但不超过32个字节。原始数据块也受到当前版本中使用int类型的限制为2GB-1字节。由于QString每个字符使用两个字节,这意味着在一个QString中只有不到2个^30个字符。
In case memory allocation fails, QString will throw a std::bad_alloc exception. Out of memory conditions in the Qt containers are the only case where Qt will throw exceptions.
当内存分配失败时,QString将抛出一个std::bad_alloc异常。Qt容器中内存不足是Qt抛出异常的唯一情况。
Note that the operating system may impose further limits on applications holding a lot of allocated memory, especially large, contiguous blocks. Such considerations, the configuration of such behavior or any mitigation are outside the scope of the Qt API.
注意,操作系统可能会对持有大量已分配内存的应用程序施加进一步的限制,尤其是持有大型连续块的应用程序。这种考虑、或行为的配置或任何缓解措施都超出了Qt API的范围。
See also fromRawData(), QChar, QLatin1String, QByteArray, and QStringRef.