Reprinted from: https://www.cnblogs.com/retry/p/9328715.html
QChar of QT
QChar class is a class used to represent a character in Qt, which is implemented in QtCore shared library. The QChar class internally represents a character in 2-byte Unicode encoding.
Coding
//char type is a built-in type in c/c + +, which describes the resolution of 1 byte memory information. char gemfield='g'; //The memory size marked by gemfield is one byte, and the information is 01100111, 8 bits. char gemfield='Chinese'; //The memory size marked by gemfield is still one byte, and the information stored is 0xBA, //This is because in windows system, Chinese characters are stored in gbk code (ANSI), and the code of "Han" is 0xBABA, because char has only one byte, so the low byte is stored. //After the above introduction of gemfield, you have understood the usage of the built-in type char, and the output of the following statement: char gemfield=0×40; printf("gemfield's value is %c",gemfield);
- QChar is the basic type of Qt processing characters, which is the encapsulation of unicode characters. QChar uses two bytes of memory and maintains an unsigned short type of memory inside it (most compilation tools will also treat it as an unsigned short type). The ucs-2 standard is used.
- It is quite easy to understand that QChar encapsulates a char type. When char type is used as the construction parameter of QChar, it will be converted to unsigned short, and then it can be taken over by QChar.
- It is also easy to understand that QChar encapsulates an unsigned short type, such as:
//According to the unicode table and codec plug-in used inside Qt, gemfield can be successfully parsed into the word "Han". QChar gemfield=0x6C49; //0x6C49 is the unicode code of "Han"
//QChar cannot deal with the following situations: QChar gemfield = 'Chinese'; //The encoding of "Han" on windows is gbk encoding with a value of 0xBABA, as mentioned above by gemfield. // The information on the unsigned short memory maintained by QChar is 0xBABA, which is not unicode encoded (the unicode encoded value is 0x6C49 mentioned above by gemfield), so it cannot be parsed. Similarly, QChar can return a character's unicode encoding through the unicode() function.
II. Qchar interface
2.1. Qchar constructor
//The QChar class provides several different prototypes of constructors for ease of use QChar(); // Construct an empty character, '\ 0' QChar(char ch); // Constructed by character data ch QChar(uchar ch); // Constructed from unsigned character data ch QChar(ushort code); // Constructed from unsigned short data code, code is Unicode QChar(short code); //Constructed from short shaped data code, code is Unicode QChar(uint code); // Constructed from unsigned integer data code, code is Unicode QChar(int code); // Constructed from integer data code, code is Unicode
2.2. Qchar character judgment
//QChar class provides many member functions to judge the type of characters, such as: bool isDigit() const; // Determine whether it is a decimal number ('0 '-'9') bool isLetter() const; // Determine whether it is a letter bool isNumber() const; // Determine whether it is a number, including sign, decimal point, etc bool isLetterOrNumber(); // Determine whether it is a letter or a number bool isLower() const; // Determine whether it is a lowercase letter bool isUpper() const; // Determine whether it is a capital letter bool isNull() const; // Determine whether it is an empty child '\ 0' bool isPrint() const; // Determine whether it is a printable character bool isSpace() const; // Determine whether it is a separator, including space, etc
2.3. Qchar data conversion
//The QChar class provides some member functions for data conversion, such as: char toAscii() const; // Get the ASCII code of the character QChar toLower() const; // Convert to lowercase QChar toUpper() const; // Convert to uppercase ushort unicode() const; // Get Unicode encoding //Note that these functions do not change the object itself, and the result of the transformation is reflected by the return value.
2.3. Character comparison of Qchar
//Some comparison operators related to QChar class are defined in Qt, such as: bool operator != (QChar c1, QChar c2); // Judge whether c1 is not equal to c2 bool operator < (QChar c1, QChar c2); // Judge whether c1 is less than c2 bool operator <= (QChar c1, QChar c2); // Judge whether c1 is less than or equal to c2 bool operator == (QChar c1, QChar c2); // Determine whether c1 is equal to c2 bool operator > (QChar c1, QChar c2); // Judge whether c1 is greater than c2 bool operator >= (QChar c1, QChar c2); // Judge whether c1 is greater than or equal to c2
2.3. Qchar and Char type conversion
//qchar - char // char Qchar::toLatin1() or char Qchar::toAscii() const char ch; qchar qch; ch = qch.toLatin1; //char -> qchar qchar(char ch) qchar (uchar ch)