Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Learn BlackBerry 10 App Development
< Prev   CONTENTS   Next >

QVariant

A QVariant acts like a union of common Qt data types. However, at any time, a QVariant can only hold a single value of a given type (however, the value itself can be multivalued such as a list of strings). Also the type stored in a QVariant must have value semantics (in other words, it must

at least define a public default constructor, a public copy constructor, and a public destructor).

A QVariant is an essential component of Cascades programming because it is used in many different scenarios, such as parsing JSON and XML files or retrieving values from a database (you will see how to parse XML using the Cascades XmlDataAccess class in Chapter 6, and JSON using the Cascades JsonDataAccess class in Chapter 7). Most importantly, the QML declarative engine uses QVariants to pass C++ types to JavaScript and vice-versa (note that this happens transparently behind the scenes). You can store your own C++ type in a QVariant by registering it with the Qt type system using the Q_DECLARE_METATYPE() macro . Listing 3-17 illustrates typical QVariant usage.

Listing 3-17. QVariant

QVariant variant = 10; if(variant.canConvert<int>()){

std::cout << variant.toInt() << std::endl;

}

variant = "Hello variant"; if(variant.canConvert<QString>()){

std::cout << variant.toString().toStdString() << std::endl;

}

// program output is

// 10

// Hello variant

Finally, you will often encounter the following QVariant-based types in Cascades development:

n QVariantList: A typedef for QList<QVariant>. Typically when parsing a JSON array, the JsonDataAccess class will return a QVariantList. You can also reference a QVariantList in QML as a JavaScript array.

n QVariantMap: A typedef for QMap<QString, QVariant>. Typically when parsing JSON objects, the JsonDataAccess class will return a QVariantMap. You can then access individual object attributes using the QVariantMap's key.

The next section will give you more information about QList and QMap.

Qt Container Classes

C++ comes with the standard library, which is a collection of generic containers and algorithms for manipulating them. However, Qt also includes its own set of container classes that can be

transparently accessed from QML. Note that the Qt container classes, just like their standard library counterparts, are class templates (in other words, you have to pass as a template parameter the type T stored in the container; you should be familiar with this if you have already used Java generics).

In Cascades programming, you will mostly use the QList and QMap containers. A QList<T> is a templated class for storing a list of values and provides fast index-based access as well as fast insertions. A QMap<Key, T> is a container for storing (key, value) pairs and provides fast lookup of the value associated with a key. You have already seen a QList in action in Listing 3-15, and Listing 3-18 gives you a quick overview of how to use a map in practice (you will also have the opportunity to see both containers in action in the code examples given in this book).

Listing 3-18. QMap

QMap<QString, int> integers; integers["one"] = 1;

integers["ten"] = 10;

integers["five"] = 5;

QList<QString> keys = integers.keys(); for(int i=0; i< keys.length(); i++){

cout << integers[keys.at(i)] << endl;

}

Note that you can store any value type in a QMap, including QVariants and pointers to QObjects.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel