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
Highlighted
Contributor
Posts: 41
Registered: ‎10-16-2012
My Device: Bold 9000
My Carrier: Telus
Accepted Solution

User editted textfield vs. code editted

I have many text fields in my app that recalculate when editted.  The issue is I only want them to recalculate when the user edits the number and not when the app inserts a result into them.  I'm using onTextChanged to fire my calculation function which works great if I could surpress it when the code itself changes the value.  It's driving me mad!  Please help!

Contributor
Posts: 41
Registered: ‎10-16-2012
My Device: Bold 9000
My Carrier: Telus

Re: User editted textfield vs. code editted

I've been experimenting with focus this morning figuring that when the user edits text the field will have focus and that will be the difference.  It does have focus during textChanging but not during textChanged.  I have found a property I can set so the field won't lose focus but am not sure how to use it.  The property is focusRetentionPolicyFlags.  I tried 

focusRetentionPolicyFlags: LoseToFocusable but that doesn't work.

Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: User editted textfield vs. code editted

Use the QObject::blockSignals function:

textField->blockSignals(true);
textField->setText("xxxxxx");
textField->blockSignals(false);
Contributor
Posts: 41
Registered: ‎10-16-2012
My Device: Bold 9000
My Carrier: Telus

Re: User editted textfield vs. code editted

How do I implement that in qml or js?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: User editted textfield vs. code editted

[ Edited ]

In QML just disconnect the signal then connect it again. Example:

http://supportforums.blackberry.com/t5/Cascades-Development/Problem-while-using-slider/m-p/2054815#M...

 


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 41
Registered: ‎10-16-2012
My Device: Bold 9000
My Carrier: Telus

Re: User editted textfield vs. code editted

Thanks for the help but I used a different technic that seems to work great in it's simplicity.  I simply use onfocuschanged method.  In the method I check for loss of focus (as opposed to gaining focus) and do my calculations then.  This way when the user changes the value and either clicks done or otherwise dismisses the keyboard the calculation fires but when the code alters the value no focus is gained or lost and therefore doesn't fire the calculation.  

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: User editted textfield vs. code editted

The onFocusChanged approach sounds like a good one. Another one that works is to simply set a property to flag whether the change is programmatic or a user change. Make a boolean flag defaulting to false, called something like "ignoreChange". Just ahead of your programmatic change, do "ignoreChange = true" and set it false just after the change. In the onTextChanged code, you can add as the first code something like "if (ignoreChange) return;" and it skips the subsequent processing during the programmatic change.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Contributor
Posts: 41
Registered: ‎10-16-2012
My Device: Bold 9000
My Carrier: Telus

Re: User editted textfield vs. code editted

Funny you mention that because that is the system I used  in my android code but it seemed to fail when ported over.  I blamed it on multiple threads but not sure exactly why it didn't work.

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: User editted textfield vs. code editted

It could be affected (broken) by having multiple threads involved, certainly. Generally you'd expect that to work only if a single thread was doing all the GUI stuff (and specifically the main Application thread, in a Cascades app...).

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!