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
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone
Accepted Solution

findChild() not working

Hi,

 

  Plese refer to the following code

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml");
		qml->setContextProperty("cppObject", cppObject);
		
	NavigationPane pane = qml->createRootObject<NavigationPane>();
		app->setScene(pane);
		Button* btn = pane ->findChild<Button*>"increaseButton");
btn = Application::instance()->findChild<Button*>"increaseButton");

 

I'm unable to get the button defined in the QML document.

 

Refer the QML doc ..

 

NavigationPane {
    id: navgationPane
    Page {
        Container {
            layout: DockLayout {
            }
            Button {
                id: increaseButton
                text: "Increase the value"
            }
       }
  }
}

 Please suggest me the possible solutions.

 

Thanks in Advance.

 

Regards,

Sha.

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

Re: findChild() not working

[ Edited ]

Hi,
Add
obectName: "increaseButton"
to Button {}

Note the quotes, they should be present.

 

Button {
  id: increaseButton
  objectName: "increaseButton"
  text: "Increase the value"
}

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: findChild() not working

[ Edited ]

Also you are missing an opening bracket.

 

Button* btn = pane ->findChild<Button*>("increaseButton");

 


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: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: findChild() not working

And finally...  Take out the Application::instance line at the bottom...

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

Re: findChild() not working

jalue,

 

Strictly speaking, as long as he follows Zmeys and my suggestions the code would still work without removing the duplicate line.

 

I assume the bracket and the duplicate line are just copy typos, it's Zmeys suggestion which is the fix to the problem.

 


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.
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: findChild() not working

Thanks very much for the support..I got the solution.

 

But technically speaking the document object should contain button. If i'm asking the qmldocument for its button child it's returning null.

 

Can you clarify my confusion?

 

 

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

Re: findChild() not working

QmlDocument is just an interface description. The objects aren't created yet.

When you call createRootObject the object tree is actually created.

So you should call findChild() on the object returned by createRootObject, not the QmlDocument itself.

In other places you can use Application::instance()->scene() to get the root object of the application and call findChild() on it.

You can also call findChild() on any child object, it will perform the search recursively.

I hope I understood the question correctly. Smiley Happy

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: findChild() not working

[ Edited ]

createRootObject is returning a pointer to the object while your code is assigning it to a new instance.

On the next line you then try and address it as a pointer again.

 

As a suggestion before posting code again, can you make sure it compiles with all the little mistakes removed.

 

 


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.
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: findChild() not working

 

@BBSJJEV sorry for that .. that's an error in copy paste... Smiley Happy

 

@JMEY That is very good desception i'm expecting. Thanks  a lot.

 


One 2 more Question's ...dont mind plz...

 

1) When creating a rootObject does the top element of the QML is returned. For suppose in my prev post it is NavigationPane. Is my understanding correct?

 

2) In some example, While creating QMLDocument they are using some thing like this

 

 QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(&app)

 

Is it required to get the parent for the document and why?

 

Thanks in advance.

 

Regards,

Sha.

 

 

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

Re: findChild() not working

1) That's correct.

 

2) In C++ every class which is created using operator new must be destroyed using operator delete, otherwise memory leak will occur. QmlDocument::create() creates the object internally as well, but the caller should destroy it.

 

So one option is to do:

QmlDocument *qml = QmlDocument::create(...);

// use qml

// destroy it, when no longer needed:
delete qml;

But in Qt there's a simpler mechanism for managing objects lifetime.

 

Every object can optionally have a parent. If the parent is set, then when the parent object is destroyed all it's children will be deleted automatically. So there will be no need to call 'delete' manually.

 

You can set the parent explicitly:

qml = ...create qml...;

qml->setParent(&app);

 

or set it using the builder pattern on initialization:

qml = QmlDocument::create("asset://main.qml").parent(&app);

 

or set it using constructor (if constructor has the appropriate parameter):

qml = new QmlDocument(&app, "asset://main.qml");

 

In the samples above we're setting qml's parent to application object instance, so it will be automatically deleted when the application is deleted.

 

Also the parent is implicitly set when adding the object to NavigationPane, to Containers etc.

 

Item *someItem = new Item;

Page *somePage = new Page;

somePage->addItem(someItem); // usually this sets someItem's parent to somePage, but check the docs of every  class to be sure. It's usually specified in the docs if the object takes ownership of the argument or not

 


Andrey Fidrya, @zmeyc on twitter