09-15-2013 08:59 AM
StackLayout *firstRowLayout = StackLayout::create().orientation(LayoutOrientatio
n::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?
Solved! Go to Solution.
09-15-2013 11:08 AM - edited 09-15-2013 11:11 AM
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
09-15-2013 12:35 PM - edited 09-15-2013 03:02 PM
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.
09-22-2013 11:24 AM
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:
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.