First, preface
Earlier we learned about some simple operations on QT strings, the classification of container classes and their main features and uses, this time we learned about some common tool classes and common controls.
ii. Qbytearry and Qvariant2.1 Qbytearry
As for Qbytearry, we have seen it in the previous article. The functions and APIs of Qbytearry and Qstring are basically similar, with many similar functions. The difference is that Qbytearry can store native binary data and 8-bit encoded text data, so what are native binary data and 8 encoded text data? A little understanding of computer principles of the children's shoes may be aware that all the data inside the computer is stored in the form of 0 and 1, which is the binary form of storage. For these binary codes, the computer does not know its specific meaning and needs to be decided by context. For a file, even if it is a text file, it can be read out in binary form, which is the binary format. If the 0 and 1 of these binaries are interpreted as one character by code, they can form a text. Thus, Qbytearry actually 楇 the native binary, and can also be seen as text, with some manipulation of the text. However, it is recommended to use qstring instead of Qbytearry for the text stream, because qstring supports Unicode.
For convenience, the Qbytearry automatically guarantees that the value after the last byte is "\", which also causes the Qbytearry to be easily converted to a const char*, as well as the stored binary data type, which can be stored in the middle of the qbytearry.
2.2 qvariant
Sometimes we want to store different types of data in a variable, if it can be implemented in Java by a variable of type object, but there is no such public inheriting class in C + +. QT by declaring a class that contains all the data types that need to be stored, this is the Qvariant class.
The Qvariant class can hold many QT data types, including Qbrush, Qcolor, Qcursor, Qdatatime, Qfont, Qkeysequence, Qpalette, Qpen, Qpixmap, Qpoint, QRect , Qregion, Qsize, and qstring, and also includes basic types, int, float, and so on.
Qvariant can also save a number of collection types, such as Qmap<qstring,qvariant>,qstringlist and qlist<qvariant>. The Item view classes, the database module and the qsettings all use the Qvariant class extensively, facilitates the reading and writing data.
Qvariant supports nested storage.
1Qmap<qstring,qvariant>map;2map["int"]=123;3map["Double"]=1.23;4map["string"]="123";5map["Color"]=qcolor (255,0,0);6Qdebug () <<map["int"]<<map["int"].toint ();7Qdebug () <<map["Double"]<<map["Double"].todouble ();8Qdebug () <<map["string"]<<map["string"].tostring ();9 //Use the value<t> () template function to get the data stored in the QvariantTenQdebug () <<map["Color"]<<map["Color"].value<qcolro> ();
We can also customize the qvariant, the data type stored by qvariant must have a default constructor and a copy constructor, and must first use the Q_declare_metatype () macro. This macro is typically placed below the header file where the class is declared.
1
1 human humen; 2 Qvariant variant = qvariant::fromvalue (humen); 3 // ... 4 if (Variant.canconvert()) { 5 Human h= variant.value( ); 6 // ... 7
three, algorithm and regular expression3.1 Qt5 Common algorithm
Common algorithms are in QT <QtAlgorithm> and <QtGlobal> modules.
1#include <QCoreApplication>2#include <QString>3#include <QtDebug>4 intMainintargcChar*argv[])5 {6 qcoreapplication app (argc, argv);7 Doublea=-19.35, b=5.6;8 DoubleC=qabs (a);//take absolute value9 DoubleMax=qmax (B,C);//Take maximum valueTen One intBi=qround (b);//take the whole A intCi=Qround (c); - - theQdebug () <<"a="<<A; -Qdebug () <<"b="<<b; -Qdebug () <<"C=qabs (a) ="<<C; -Qdebug () <<"QMax (b,c) ="<<Max; +Qdebug () <<"Bi=qround (b) ="<<bi; -Qdebug () <<"Ci=qround (c) ="<<ci; + A at Qswap (BI,CI); //Exchange value -Qdebug () <<"Qswap (BI,CI):"<<"bi="<<bi<<"ci="<<ci; - - returnapp.exec (); -}
3.2 Basic Regular Expressions
The Qregexp class of Qt is the expression class of regular expressions, based on Perl's regular expression language, which fully supports Unicode, and can easily perform some operations such as validation, lookup, substitution, and segmentation.
The regular expression consists of an expression (expressions), a quantifier (quantifiers), and an assertion (assertions).
(1) The simplest expression is a character. For example, the expression I want to represent a character set can use [aeiou] to denote a match for all the larger reason letters, and [^aeiou] to match all non-vowels, and a sequential character set that uses an expression [a-z] to match all lowercase letters.
(2) quantifiers indicate the number of occurrences of an expression, such as "x[1,2", which indicates that X has at least one, with a maximum of two.
In computer languages, identifiers usually require a letter or underscore, followed by letters, numbers, and underscores. Like "[a-za-z_]+[a-za-z_0-9]*"
+ means [a-za-z_] appears at least once and can occur multiple times
* means [a-za-z_0-9] can occur 0 or more times
Quantifiers for regular expressions
| Quantifiers |
Meaning |
Quantifiers |
Meaning |
| E? |
Match 0 or 1 times |
E[n,] |
Match at least N times |
| E+ |
Match 1 or more times |
E[,M] |
Matches up to M times |
| e* |
Match 0 or more times |
E[N,M] |
Matches at least n times, up to M times |
| E[n] |
Match n Times |
|
|
The character set of the regular expression
| Object |
Significance |
| C |
A single character identifies itself, unless it has a special meaning in a regular expression |
| \c |
Special characters themselves |
| \a |
Match ASCII Bell (BEL,0X07) |
| \f |
Match ASCII form feed (ff,0x0c) |
| \ n |
Match ASCII line (Lf,0x0a,unix newline) |
| \ r |
Match ASCII carriage return (cr,0x0d) |
| \ t |
Match ASCII horizontal Tab key (HT,0X09) |
| \v |
Match ASCII vertical Tab key (VT,0X0B) |
| \xhhhh |
Match Unicode character hexadecimal number hhhh (0X0000-0XFFFF) |
| \0ooo |
Matching octal number ooo (0-0377) |
| . |
Match any character (including newline) |
| \d |
Matching numbers (Qchar::isdiagit ()) |
| \d |
Match non-numeric |
| \s |
Match empty characters (Qchar::isspace ()) |
| \s |
Match non-whitespace characters |
| \w |
Match the word character (Qchar::isletterornumber (), Qchar::ismark (), or ' _ ') |
| \w |
Match non-word characters |
| \ n |
Match the first few backreference such as \1,\2 |
(3) "^" "$" "\b" are all assertions of regular expressions
Assertions for regular expressions
| Symbol |
Meaning |
Symbol |
Meaning |
| ^ |
Represents a match at the beginning of a string |
\b |
Non-word boundary |
| $ |
Indicates a match at the end of a string |
(? =e) |
Represents an expression immediately after E matches |
| \b |
Word boundaries |
(?! E |
Indicates that the expression does not follow the E-match immediately after |
For example, if the using is matched only if the using is followed by namespace, you can use the using (? =e\s+namespace) (here). "\s" after "=e" means matching a white space character).
If you use the using (?! E\s+namespace) ", the expression matches the using only if the using is not followed by a namespace.
If you use "Using\s+namespace", the match is a using namespace.
The C + + compiler escapes the backslash, contains \ In the regular expression, needs to be written \ \, to match the backslash character itself, type four times \\\\.
The knowledge of the regular is quite a lot, because I have not really used QT's regular, here first dug a pit, after a very good example of time to fill up, hey. Here is a good summary of the blog, hoping to help you. Click here to jump ...
My QT5 Learning Path (iii)-Template Library, tools class, and controls (middle)