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

Using the Document Context

If you carefully study the Option class given in Listing 3-6, you will notice that we have already defined the class in such a way that it can be readily used from QML. In fact, just like the QDeclarativePropertyMap instance, all you simply need to do is to add an Option instance to the QML document context property from C++ (see Listing 3-25 and Listing 3-26).

Listing 3-25. ApplicationUI.hpp

class ApplicationUI : public QObject

{

Q_OBJECT

public:

ApplicationUI(bb::cascades::Application *app); virtual ~ApplicationUI() { }

private:

Option* m_option;

};

Listing 3-26. ApplicationUI.cpp

ApplicationUI::ApplicationUI(bb::cascades::Application *app) : QObject(app), m_option(new Option(this))

{

// 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);

qml->setContextProperty("_option", m_option);

// Create root object for the UI

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

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

}

The main.qml document referencing the Option instance is given in Listing 3-27.

Listing 3-27. main.qml

import bb.cascades 1.2 Page {

Container {

//Todo: fill me with QML

Label {

text: "Option Pricer"

horizontalAlignment: HorizontalAlignment.Center textStyle.base: SystemDefaults.TextStyles.BigText

}

TextField {

id: spotField

hintText: "Enter spot price" onTextChanging: {

_option.spot = text;

}

}

TextField {

id: strikeField

hintText: "Enter strike price" onTextChanging: {

_option.strike = text;

}

}

TextField {

id: maturityField

hintText: "Enter time to maturity" onTextChanging: {

_option.maturity = text;

}

}

TextField {

id: volatilityField

hintText: "Enter underlying volatility" onTextChanging: {

_option.volatility = text;

}

}

TextField {

id: riskfreeRateField

hintText: "Enter risk free rate" onTextChanging: {

_option.riskfreeRate = text;

}

}

Label {

text: "Option fair price"

horizontalAlignment: HorizontalAlignment.Center

}

TextField {

id: priceField text: _option.price

}

}

}

Here is a brief description of the code shown in Listing 3-27:

n The TextFields' textChanging signals are used to update the corresponding

Option object's properties.

n As mentioned previously, when any of the option's properties is updated, an

Instrument::priceChanged() signal is also emitted by the Option.

n The priceField's text property is bound to the corresponding Instrument::price property (the QML declarative engine will therefore update the QML property when the Instrument::priceChanged() signal is emitted).

The resulting application UI is given in Figure 3-1.

Figure 3-1. Option pricer UI

 
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