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
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

Where should I call qmlRegisterType

In my class lib, class A uses enums defined in class B. When I use A in qml, do I have to call qmlRegisterType for B in applicaiton class? Can I call qmlRegisterType for B in A, like in A's constructor?

Regular Contributor
Posts: 56
Registered: ‎06-25-2012
My Device: non-blackberry user
My Carrier: Airtel

Re: Where should I call qmlRegisterType

if you want to use class B in qml then you need to call qmlRegisterType, otherwise if you have already exposed class A to qml i dont think you need to expose B again !!
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

Re: Where should I call qmlRegisterType

I called qmlRegisterType for A in application class. But, some of A's properties are enums defined in B. When I tried to set them in QML, I can't see any setter calls in debug mode. But, in my Library, the user uses either A or B. they never need to use both, as A is a kind of wrapper for B. I don't want to redefined those enums in A. Otherwise, I need to write extra conversion code. So, I wonder if I can qmlRegisterType B some where else, such as in A.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Where should I call qmlRegisterType

You should do it before the QML page which uses it is created, for that reason and to keep it simple I would just register your types before the main QML page is instantiated.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Regular Contributor
Posts: 56
Registered: ‎06-25-2012
My Device: non-blackberry user
My Carrier: Airtel

Re: Where should I call qmlRegisterType

i have never exposed enums in from c++ to QML my bad , but a short solution would be to expose a property in class A and based on the property values why dont you toggle enums from class B !!
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

Re: Where should I call qmlRegisterType

Is it ok to call qmlRegisterType for a class more than once? For example, in A's constructor, call qmlRegisterType to register B. 

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Where should I call qmlRegisterType

I don't see why not.

 

If you want to understand what exactly is happening when you do a qmlRegisterType I suggest you take a look at this...

 

http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-definetypes.html

 

However I think you are overcomplicating things, if all you want to do is use the enums in your QML, registering the class and its enums within your applicationui.cpp before the qml instantiation should be all you need to do. It doesn't matter then whether you are using it to pass a value to another class or not.

 

I do this in my apps that way the type is available to all classes.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

Re: Where should I call qmlRegisterType

I am creating a static class library for others to use. I don't want the user to do a lot of things to use this lib, such as calling qmlRegisterType for a lot of public classes.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Where should I call qmlRegisterType

[ Edited ]

Ok understood, at this point then could you post a simple example of the code you have so far?
As long as the class constructor is called before the enum is used in the QML then I see no reason for it not to work so we will need to look at your code.

 

[Edit] Alternatively do all your registering in a single function and inform your developers the call needs to be made before the QML is created.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Where should I call qmlRegisterType

Why not create a single initialization function which will register all the classes? User will have to call it only once.

Registering the class in constructor makes little sense because user won't be able to declare a variable holding a value of B::enum before instantiating the class A. It's better to register all classes before instantiating the QML file.

Andrey Fidrya, @zmeyc on twitter