We mentioned the QByteArray class in the last section of QString. Now we will first introduce this class.
QByteArray has APIs similar to QString. It also has corresponding functions, such as left (), right (), mid () and so on. These functions not only have the same name as QString, but also have almost the same function. QByteArray can store native binary data and 8-bit encoded text data. How can this sentence be understood? We know that all data in the computer is stored in the form of 0 and 1. This form is binary. For example, if a string of 0 and 1 code: 1000, the computer does not know what it represents, which is determined by the context: it can be an integer 8, it can also be an ARGB color (to be precise, the integer 8 encoding is not that simple, but let's take a look at it ). For a file, even a text file can be read in binary format. This is the binary format. If the strings 0 and 1 in binary are interpreted as characters by encoding, they are in the text format. Therefore, QByteArray is actually a native binary, but can also be considered as text, so it has some operations on text. However, we recommend that you use QString to represent text. The important reason is that QString supports Unicode.
For convenience, QByteArray automatically ensures that "the bit after the last byte" is '\ 0 '. This makes it easy to convert QByteArray to const char *, which is the two functions we mentioned in the previous chapter. Similarly, as a native binary storage, QByteArray can also store '\ 0' in the middle, instead of' \ 0' In the last bit.
In some cases, we want to store data in a variable. For example, I have an array that stores both integers, floating-point numbers, and strings. For Java, it is very simple to declare this array as Object [] type. What does this mean? In fact, inheritance is used here. In Java, int and float are both native data types, but they all correspond to one packaging class Integer and Float respectively. All these integers, Float, and String are inherited from the Object. That is to say, Integer, Float, and String are all one (that is, the relationship between is-a) Object. In this way, object arrays can store different types. However, C ++ does not have such an Object class because Java is a single Object, but C ++ is not. In Java, all classes can be traced back to the Object class, but C ++ does not have such a root. So how can this operation be implemented? One way is to save them as string classes. For example, if int I = 10, I will store the "10" string. A simple data type is acceptable, but is it more complicated? For example, a color? Do you want to convert all the values of ARGB into strings? This method is complex and has no advantages such as C ++ type checks. So we can try another way: Create an Object class, which is a "very big" class and stores almost all data types, such as the following code:
- class Object
- {
- public:
- int intValue;
- float floatValue;
- string stringValue;
- };
How about this class? It is enough to store int, float, and string. Well, this is our idea and the idea of Qt. In Qt, such a class is QVariant.
QVariant can save many Qt data types, including QBrush, QColor, QCursor, QDateTime, QFont, QKeySequence, QPalette, QPen, QPixmap, QPoint, QRect, QRegion, QSize, and QString, there are also basic types of C ++, such as int and float. QVariant can also save many set types, such as QMap <QString, QVariant>, QStringList, and QList <QVariant>. Item view classes, database modules, and QSettings all use the QVariant class to facilitate data reading and writing.
QVariant can also be nested for storage, such
- QMap<QString, QVariant> pearMap;
- pearMap["Standard"] = 1.95;
- pearMap["Organic"] = 2.25;
-
- QMap<QString, QVariant> fruitMap;
- fruitMap["Orange"] = 2.10;
- fruitMap["Pineapple"] = 3.85;
- fruitMap["Pear"] = pearMap;
QVariant is used to build Qt Meta-Object, so it is part of QtCore. Of course, we can also use it in the GUI module, for example
- QIcon icon("open.png");
- QVariant variant = icon;
- // other function
- QIcon icon = variant.value<QIcon>();
We use the value <T> () template function to obtain data stored in QVariant. This function is also applicable to non-GUI data. However, in non-GUI modules, we usually use a series of... () function, such as toString.
If you think that QVariant provides too few storage data types, you can also customize the storage type of QVariant. The data type stored by QVariant requires a default constructor and a copy constructor. To implement this function, you must first use the Q_DECLARE_METATYPE () Macro. This macro is usually placed under the header file where the class declaration is located:
- Q_DECLARE_METATYPE(BusinessCard)
Then we can use:
- BusinessCard businessCard;
- QVariant variant = QVariant::fromValue(businessCard);
- // ...
- if (variant.canConvert<BusinessCard>()) {
- BusinessCard card = variant.value<BusinessCard>();
- // ...
- }
Due to the limitations of the VC 6 compiler, these template functions cannot be used. If you use this compiler, you need to use qVariantFromValue (), qVariantValue <T> () and qVariantCanConvert <T> () these three macros.
If the <and> operator is overwritten for the custom data type, it can be directly used in QDataStream. However, you must first use qregisterpolicypestreamoperators <T> (). Macro for row registration. This allows QSettings to operate data using operators, such
- qRegisterMetaTypeStreamOperators<BusinessCard>("BusinessCard");
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/276235