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

Exposing C++ Objects to QML

There are essentially four ways of exposing C++ objects to QML:

n You can use a QDeclarativePropertyMap to aggregate values in a map, and then set it as a context property of the QML document.

n You can selectively expose properties and methods from a QObject derived class, and then set the instance as a context property of the QML document.

n You can “attach” an instance of a QObject to a QML UIObject object using its UIObject::attachedObjects property in QML. Note that you will have to first register the QObject derived class with the QML type system.

n You can create a QML custom control in C++ by extending bb::cascades::CustomControl. You can then use the control as any other QML element in your document. Once again, you will have to register your control with the QML type system.

Before getting into the details of exposing C++ objects in practice, let's take a detailed look at the application delegate's constructor and explain the flow of events (see Listing 3-22). (We conveniently skimmed over this in Chapters 1 and 2, but now it is time to get our feet wet).

Listing 3-22. ApplicationUI.h

ApplicationUI::ApplicationUI(bb::cascades::Application *app) : QObject(app)

{

// prepare the localization

// code omitted here

// Create scene document from main.qml asset, the parent is set

// to ensure the document gets destroyed properly at shut down. QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

// Set the qml document context properties before creating root object using:

// void QMLDocument::setContextProperty(const QString &propertyName, QObject *object)

// Create root object for the UI

AbstractPane *root = qml->createRootObject<AbstractPane>();

// Set created root object as the application scene app->setScene(root);

}

Here is a step-by-step description of the code shown in Listing 3-22:

n QmlDocument::create(const QString &qmlAsset, bool autoload= true) is called and a QML document is loaded from the assets folder of your application. All documents loaded with this method will share the same QML declarative engine, which is an instance of a Qt QDeclarativeEngine.

n A context is also associated to the document. Contexts allow data to be exposed to components instantiated by the QML declarative engine (all documents loaded using the QmlDocument::create() method share the same instance of the declarative engine, which is associated with the application).

n Contexts form a hierarchy and the root of the hierarchy is the QML declarative engine's context. The context associated with the loaded document is therefore derived from the root context and shares its properties. Note that these properties are not the ones corresponding to QObject but the ones set with QDec larativeContext::setContextProperty(const QString &, QObject *) method. You also have to be aware that you will override a property from the root context if you set it with a different value in the document context.

n A root node is instantiated for the scene graph represented by the QML document by calling the QmlDocument::createRootObject<T>() template method (the template T parameter must be pointer to a UIObject subclass).

n During the instantiation of the root node, the UIObject::creationCompleted()

signal will be emitted for all UIObjects in the scene graph.

Now let's look at how the document context is used in practice for exposing C++ objects.

 
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