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

Exposing QObjects

As explained in the previous section, using QDeclarativePropertyMap is a great way to expose data structures based on common QML “basic types.” There will be times, however, where you will need to expose your own C++ objects directly so that you can achieve more complex behaviors, such as calling the object's methods or handling its signals in QML (or vice-versa, let the object handle signals emitted from QML). Typically, such objects play the role of application delegates or service façades (I will tell you more about this in the section dedicated to the model-view-controller pattern).

When exposing some functionality to QML, you should always think in terms of services and granularity. For example, if you need to access a large C++ library from QML, it is often preferable to define a set of coarse-grained services that you expose to the QML layer instead of trying to expose every single class of your library. By doing so, you will be able to define clear boundaries between the QML layer and your C++ types. This will also avoid leaking the internals of your class library

to the QML layer, thus providing the additional benefit of decoupling your UI logic from the C++ application logic. Once you have decided on your services' granularity, you will be able to design your QObject based C++ service classes using the following recipe:

n Identify the class properties that you want to access from QML.

n Identify the class signals that you want to handle in QML.

n Identify any slots and class methods that should be called from QML.

n When implementing your class methods, use types that you can pass as

QVariants.

In practice, in order to expose a C++ class instance to QML, you need to do the following:

n Add the Q_OBJECT macro at the start of the class declaration (and, of course, your class must inherit from QObject).

n Use the Q_PROPERTY macro in order to expose class properties to QML.

n Use the Q_INVOKABLE macro in order to expose class methods to QML.

n Signals and slots are automatically exposed using the signals: and slots:

annotations, as explained in Chapter 1.

The syntax for declaring object properties with the Q_PROPERTY macro is as follows:

Q_PROPERTY(type name

READ getFunction [WRITE setFunction] [RESET resetFunction] [NOTIFY notifySignal] [DESIGNABLE bool] [SCRIPTABLE bool]

[STORED bool] [USER bool] [CONSTANT] [FINAL])

The only mandatory values are the property type, name, and the getter function for reading the property. In practice, you will be using a much shorter version of the macro:

Q_PROPERTY(type name READ getFunction WRITE setFunction NOTIFY notifySignal)

 
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