Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1
My Carrier: rogers

qmlRegisterType vs qml->setContextProperty?

Hey gang,

My project makes use of a c++ class which is exposed to the qml based ui.

I've been experimenting with both qmlRegisterType and qml->setContextProperty.

Both seem to work well.

I can't figure out why you'd use one over the other.

What's the difference between them?

Thx,

J
bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Please use plain text.
Contributor
adrz1
Posts: 11
Registered: ‎10-15-2012
My Device: BB10 Alpha Dev B
My Carrier: TIGO

Re: qmlRegisterType vs qml->setContextProperty?

qml->setContextProperty exposes an object to qml, you can use it if you want to pass an instance of your model, controller, gateway or any class that you generally has a unique instance and want to access to it from qml.

 

qmlRegisterType tells qml about your object structure, so you can create objects directly from qml language. for instance:

import "MyModel" 1.0

 

Item {

  MyModel { }

}

 

Generally you have to register types when you want to manipulate objects in an ListView.

Please use plain text.
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1
My Carrier: rogers

Re: qmlRegisterType vs qml->setContextProperty?

Thanks for the answer. Though I'm still not totally clear why you'd use one over the other.

Is it that setContextProperty creates a single instance of the object whereas RegisterType allows you to create one *or more* instances of an object?
bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Please use plain text.
Contributor
adrz1
Posts: 11
Registered: ‎10-15-2012
My Device: BB10 Alpha Dev B
My Carrier: TIGO

Re: qmlRegisterType vs qml->setContextProperty?

when you use setContextProperty you are putting an instantiated object that will be accesible through qml.

when you yse RegisterType you are telling qml about your class structure, so qml can create and manipulate objects of your class.

 

If you are only calling properties, methos or slots of an instantiated object, you only need setContextProperty, if you want to create instances, e.g. a custom ui component o you are using your C++ class to show a list of data (the list component requires to know about your class structure)

Please use plain text.