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
Posts: 95
Registered: ‎02-23-2011
My Device: Z10 STL100-1
My Carrier: IM3

Does this code consume performance too much?

I set a class as Context Property in main.cpp

Then, in my QML, I have a JavaScript function that returns a property of said class. This function is set as the value of a Label's text property

I noticed that the Label updates its value in real time without me doing anything else. What I want to ask is: does the code executes said JavaScript function all the time (thus updating it in real time) or does the code only execute said function once and the value auto-updates by itself without further processing power?
BlackBerry Z10
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Does this code consume performance too much?

[ Edited ]

When binding a C++ property (Q_PROPERTY) in QML it uses a signal specified in property's NOTIFY parameter to determine if variable has changed:

 

Q_PROPERTY(int myValue READ value NOTIFY valueChanged)

 

The expression is re-evaluated only when valueChanged signal is emitted, which is usually done in setter when the value changes. In other words, the code will be executed only once if setter is properly implemented.

 

This even works when the property is binded indirectly via JavaScript function.

 

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 95
Registered: ‎02-23-2011
My Device: Z10 STL100-1
My Carrier: IM3

Re: Does this code consume performance too much?

I don't bind it with Q_PROPERTY. Rather, I use setContextProperty to have the class accessible.

Does it exhibit the same behavior?
BlackBerry Z10
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Does this code consume performance too much?

[ Edited ]

setContextProperty makes an instance of the class accessible in QML. Properties & methods should be properly declared to be accessible from QML. If you aren't using properties, can you show some code please? I thought you were referencing properties of that class from JavaScript function in QML.

 

If you're exporting one of standard classes using setContextProperty, this sometimes works because in many classes  properties already have corresponding Q_PROPERTY declarations.


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 95
Registered: ‎02-23-2011
My Device: Z10 STL100-1
My Carrier: IM3

Re: Does this code consume performance too much?

On mobile here, can't post code until I got back to PC, but I'll try to be clearer.

First, in main.cpp, I declare a setContextProperty with a class (say, customClass), accessible in QML as "_customClass". This class has properties 'name', 'address', 'phone'.

In my QML, I create a JavaScript function "giveName" with only one line: "return _customClass.name"

Then, I create a Label in QML, and set its text property value as giveName()

The Label seem to update its value automatically whenever I change the name value in that class
BlackBerry Z10
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Does this code consume performance too much?

Thanks for clarifying. I guess name, address and phone are declared as Q_PROPERTY-s in class declaration, otherwise QML won't see them.

If they aren't declared in header file, then possibly they are manipulated directly using QObject's setProperty method:
setProperty("name", "value");
This should work properly as well.

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 95
Registered: ‎02-23-2011
My Device: Z10 STL100-1
My Carrier: IM3

Re: Does this code consume performance too much?

So, they will exhibit the same behavior (auto-update without continuous processing)?

I'm thinking of optimizing my app with signal-based updates. But if that is the default behavior, then it's as optimized as it can be
BlackBerry Z10
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Does this code consume performance too much?

Yes. You can verify that by adding console.log("...") to JavaScript function to check how often it's called. It shouldn't be called in loop and the updates are signal-based.

Andrey Fidrya, @zmeyc on twitter
New Member
Posts: 3
Registered: ‎05-08-2013
My Device: z10
My Carrier: blackberry

Re: Does this code consume performance too much?

[ Edited ]

mail me code Removed personal information to comply withCommunity GuidelinesandTerms and Conditions of Use.

 

i can help you