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
Regular Contributor
Posts: 63
Registered: ‎08-05-2013
My Device: Blackberry-10 webworks
My Carrier: zcon

Set focus on textfield in qml from cpp dynamically

Hi,

I have three textfields in qml file. After submitting the data according to the web service response I need to set the focus on the particular textfield from cpp file. As my textfields are in qml I am not able to place focus on particular textfield from cpp.

I am not able to use requestFocus() function in cpp.

Please help me..

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

Re: Set focus on textfield in qml from cpp dynamically

You can use the findChild() method to locate the approproiate TextField and then call the requestFocus function...

 

http://developer.blackberry.com/native/reference/cascades/qobject.html#findChild

 

 


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: 63
Registered: ‎08-05-2013
My Device: Blackberry-10 webworks
My Carrier: zcon

Re: Set focus on textfield in qml from cpp dynamically

yes,  I have used findChild() to get textfield object but not able to call requestFocus function.

 

QmlDocument *qml = QmlDocument::create("asset:///Register.qml");
						AbstractPane *root = qml->createRootObject<AbstractPane>();
						QObject *textfield = root->findChild<QObject*>("phone");



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

Re: Set focus on textfield in qml from cpp dynamically

[ Edited ]

Because requestFocus does not reside in the QObject class it resides in the control class!

 

Change QObject to TextField in your findChild call;

 

TextField* textfield = root->findChild<TextField*>("phone");

textField->requestFocus();

 Make sure you are correctly setting the UI controls objectName as well.


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: 63
Registered: ‎08-05-2013
My Device: Blackberry-10 webworks
My Carrier: zcon

Re: Set focus on textfield in qml from cpp dynamically

hey,

 

still not able to place the focus. Do I need to use setScene() function. But wile using setScene() my page get refresh and all textfields are empty.

 

current code is

QmlDocument *qml = QmlDocument::create("asset:///Register.qml");
qml->setContextProperty("RoamAssist",this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
TextField* textfield = root->findChild<TextField*>("ph");
textfield->requestFocus();

 

What can I do?

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

Re: Set focus on textfield in qml from cpp dynamically

Please check to see that the findChild function is returning an object, I would bet large amounts of money it is returning null.

 

If this is the case then you have either not set your objectName correctly or the object is not in the scene graph at the time you are doing the call.

 

 

 


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: 63
Registered: ‎08-05-2013
My Device: Blackberry-10 webworks
My Carrier: zcon

Re: Set focus on textfield in qml from cpp dynamically

Using findChild I am able to get the textfield but not able to display those chages on screen. If I use setScence() function then hole page is reloaded and previous entered values are gone.

How can I set focus to textfield?