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

Using the attachedObjects Property

I am now going to show you how to use the Option class as a UIObject's attachedObjects property. You first need register the Option class with the QML type system (usually, you will do this in main.cpp; see Listing 3-28).

Listing 3-28. main.cpp

Q_DECL_EXPORT int main(int argc, char **argv)

{

qmlRegisterType<Option>("ludin.instruments", 1, 0, "OptionType"); Application app(argc, argv);

// Create the Application UI object, this is where the main.qml file

// is loaded and the application scene is set. new ApplicationUI(&app);

// Enter the application main event loop. return Application::exec();

}

The call to qmlRegisterType<Option>("ludin.instruments", 1, 0, "OptionType") effectively registers the Option C++ type with the QML type system and the corresponding QML type OptionType.

To actually use the type in main.qml, you need to import the ludin.instruments namespace and declare an OptionType object as a UIObject's attachedObjects property (see Listing 3-29).

Listing 3-29. OptionType

import bb.cascades 1.2

import ludin.instruments 1.0

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"

}

TextField {

id: strikeField

hintText: "Enter strike price"

}

TextField {

id: maturityField

hintText: "Enter time to maturity"

}

TextField {

id: volatilityField

hintText: "Enter underlying volatility"

}

TextField {

id: riskfreeRateField

hintText: "Enter risk free rate"

}

Label {

text: "Option fair price"

horizontalAlignment: HorizontalAlignment.Center

}

TextField {

id: priceField text: option.price

}

attachedObjects: [ OptionType {

id: option

type: OptionType.CALL symbol: "myoption" spot: spotField.text

strike: strikeField.text maturity: maturityField.text volatility: volatilityField.text

riskfreeRate: riskfreeRateField.text

}

]

}

}

Using Bindings

You should note that unlike Listing 3-28, you are not using signals and slots to update the controls in the scene graph. In fact, everything is done using bindings and the net result is that the UI code is mostly declarative. As illustrated in the code, the QML OptionType object's properties are bound to the corresponding TextFields' text properties. Similarly, the priceField's text property is bound to the OptionType object's price property (note that the QML declarative engine automatically transforms the numeric value of the price property into a string before setting the TextField's text property). Whenever a property changes in C++, the QML declarative engine updates the corresponding bound property in QML. In other words, by using bindings, you have delegated the mundane task of updating your application's controls' to the QML declarative engine (this also results in cleaner QML requiring less maintenance).

 
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