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

Import Statements

Import statements tell the declarative engine which libraries, resources, and component directories are used in a QML document. An import statement will then do any of the following:

n Load a versioned module containing QML registered types. This is how you import the Cascades core controls module in the global namespace (this is also how C++ types are exposed to QML through the qmlRegisterType method, as we will see in Chapter 3).

n Specify a directory relative to your application's assets directory, which contains type definitions in QML documents. As a result, all the QML object types defined in the directory will be imported in the global namespace.

n Load a JavaScript file containing functions that you want to use in your QML document.

When using an import statement, you can further use a qualifier as a local namespace identifier. This is mostly used when importing a JavaScript file or when you want to make sure that there will be no clashes with types declared in the global namespace. Listing 2-2 shows a few examples:

Listing 2-2. imports

import bb.cascades 1.2

import "mycontrols"

import "mycontrols/core" as MyCoreControls import "parser.js" as Parser

The first line imports the versioned bb.cascades library (or module) in the global QML document namespace. The second line imports all the QML types defined in the mycontrols directory in the global QML namespace. The third example imports the QML types defined in the mycontrols/ core directory and binds them to the local MyCoreControls namespace. A type SomeType will then be accessible using MyCoreControls.SomeType. This is essentially a way of avoiding clashes when

importing controls with the same name from different modules (for example, if you have defined your own Label control in mycontrols/core, then it will not clash with the Cascades control with the same name and yours will be accessible using MyCoreControls.Label).

Object Declarations

In main.qml, a block of QML code defines a scene graph of objects to be created by the runtime engine. An object is declared in QML using the name of its object type followed by a set of curly braces. The object's attributes are then declared in the body. An object's attribute can in turn be another object declaration. In this case, you simply need to reapply the same rules for declaring that attribute. Listing 2-3 extends the example given in Listing 2-1 to show you how this works in practice.

Listing 2-3. main.qml

import bb.cascades 1.2

Page {

id: mainscreen content: Container {

id: maincontainer controls: [

Button {

id: first

text: "Click me!"

},

Button {

id: second text: "Not me!"

}

]

}

}

The Page control represents a device's screen. Its content property, the root control, is usually a cascades Container core control. A Container can in turn include child controls as well as other Containers by setting its controls property (note that the property values are specified in brackets ([ ]), indicating that this is a QML list). As you will see later in this chapter, QML objects also have

a predefined id property, which is useful when you want to reference them from JavaScript code. The page declaration in Listing 2-3 is a bit verbose and you can actually make it shorter by avoiding explicitly declaring default properties. A property can be marked as “default” in the QML object type definition, and whenever you declare an object without assigning it to a property, the QML engine will try to assign it to the parent object's default property (the default property for the Page control is content and the default one for Container is controls). Listing 2-4 gives an updated version of main. qml using default properties.

Listing 2-4. main.qml with Default Properties

import bb.cascades 1.2 Page {

id: mainscreen

Container {

id: maincontainer Button {

id: first

text: "Click me!"

}

Button {

id: second text: "Not me!"

}

}

}

 
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