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
Developer
Posts: 163
Registered: ‎12-06-2012
My Device: Z10, Q10
My Carrier: BlackBerry
Accepted Solution

Will deleting a UI control also deletes its children?

StackLayout *firstRowLayout = StackLayout::create().orientation(LayoutOrientation::LeftToRight);
	Container *firstRowContainer = Container::create().layout(firstRowLayout);
	Button *locationButton = Button::create().text("Location");
	TextArea *locationTextArea = TextArea::create().text("Heloo Hi How aer you ...thsi is for location based label. Hope this is long enough. well I don't think so but whaever");
	firstRowContainer->add(locationButton);
	firstRowContainer->add(locationTextArea);

 if I delete the reference of firstRowContainer will it also delete the layout,button and the textArea also and free the memory? 

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

Re: Will deleting a UI control also deletes its children?

[ Edited ]

Yes, 'delete firstRowContainer' will delete everything.

 

I usually format it this way so the hierarchy is apparent:

 

Button *locationButton;
TextArea *locationTextArea;

Container *firstRowContainer = Container::create()
  .layout(StackLayout::create()
    .orientation(LayoutOrientation::LeftToRight))
  .add(locationButton = Button::create()
    .text("Location"))
  .add(locationArea = TextArea::create()
    .text("Heloo Hi How aer you ...thsi is for location based label. "
          "Hope this is long enough. well I don't think so but whaever"))
);

QObject::connect(locationButton, .....); etc

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 163
Registered: ‎12-06-2012
My Device: Z10, Q10
My Carrier: BlackBerry

Re: Will deleting a UI control also deletes its children?

[ Edited ]

So if my my class inherits QObject. And if I set this class as parent for all the high level UI controls then I don't have to explicitly delete any of the UI objects in the class' destructor right? Because when my class is destructed then all the high level UI controls will be destroyed. With them their children UI controls will also be destroyed. Like I delete my class and then all the Pages and also the UIs added to it wil aslo be destroyed automatically. That is a really really useful feature.

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

Re: Will deleting a UI control also deletes its children?

That's correct.

 

In the case of GUI controls the best practice is to set the container of every item as their parent (in most classes, this is done by default if the parent is not set and the item is added to the container).

 

For example, if you need to create a Container holding a Label, define the class as

 

"class MyControl: public bb::cascades::CustomControl",

In the constructor create a Container and assign to CustomControl (it's parent will now be CustomControl).

Create another control, add it to the container (the container will now be the control's parent) etc

The hierarchy will look like this: MyControl -> Container -> Label

 

When MyControl is destroyed, the entire tree of objects will be destroyed as well. Custom destructor is not needed.

 

 Note that it's recommended to inherit only from CustomControl. While directly inheriting from Container works in practice, the documentation discourages this and compatibility between versions is not guaranteed. More info on this thread:

http://supportforums.blackberry.com/t5/Native-Development/Is-subclassing-Container-acceptable/m-p/20...

and

http://supportforums.blackberry.com/t5/Native-Development/Coming-from-Android-is-there-a-something-s...

 


 

As for non-GUI classes, any hierarchy of classes inherited from QObject can be built. It's ok to create an instance of a class holding some data and attach it to GUI object, but I recommend against doing the opposite: attaching GUI objects to non-GUI objects as it's very easy to mess up the hierarchy.

 

For example, if you set Label's parent to some custom object X, add the Label to Container Y, then delete the parent object X, the app will crash. Deleted objects aren't automatically removed from their containers.

 

But if the Container is Label's parent, it's ok to delete the container. The label will be deleted too, and the container will no longer reference the label because the container was itself deleted.

 


Andrey Fidrya, @zmeyc on twitter