What do you mean "better"? They are perfectly equivalent. The second one is less code to type in. If that counts as "better" then it's better.
What do you mean "better"? They are perfectly equivalent. The second one is less code to type in. If that counts as "better" then it's better.
hey, i was wondering if in the second fragment, its done wrongly. cuz 'this' has been set as the parent of the dialog and as every parent deletes its childs when deleting themselves, when it will try to delete the dialog , the app should crash cuz the dialog is actually created on stack..
no, that wont make difference..all i mean is widgets and dialogs should always be created on heap..AFAIK
No, that's not true. A modal dialog will always be destroyed before its parent. Unless of course you explicitely shoot yourself in the leg but then the dialog shouldn't have been modal. It really doesn't matter which way you choose here. The stack based version is simply a few statements shorter to code and a tiny bit faster to execute.
so will that concept apply even in the case of a normal dialog(non-modal)?
One goes out of scope by itself, one doesn't automatically go out of scope...
If it is a modal dialog, the 2nd one works as good as the 1st one but with less codes... You can't say one is better than the other, it depends on situation...
Does this forum has any utilities to save a thread, such add to favouites?
ok, so that means when i have a model dialog on STACK with another widget as parent..thats not wrong? one would think it should be...when parent will try to delete it, it should be crashing time..or am i getting smth really wrong about model dialog??.or maybe u can create a model dialog on stack and not give it a parent..in these examples, 'this' is parent, so be a dialog model or not, dialog creation on stack should be wrong.
No, it's not. Lifespan of a modal dialog is short - most probably it is born and dies within the same function (method) call. So there is no risk the child will live longer than its parent because it is created on the stack within one of its parent's (sub)methods.
Yes, it would crash. But the parent won't try to delete it because it opened the dialog for a reason and it wants to react on the choice/action performed by the user within the modal dialog.one would think it should be...when parent will try to delete it, it should be crashing time..or am i getting smth really wrong about model dialog??
Yes, you can. But then the dialog will not really be modal..or maybe u can create a model dialog on stack and not give it a parent..
No, it's fine :-) The parent lives longer than the child so the child will detach itself from the parent when it goes out of scope and is destroyed thus the parent will not try to delete it when it (the parent this time) goes out of scope.in these examples, 'this' is parent, so be a dialog model or not, dialog creation on stack should be wrong.
ok, i can follow this chain of thoughts..but one thing still bugs me..i read on some of the threads here that one should ALWAYS create widgets and dialogs on heap, layouts also..cuz once they go out of scope and parents try to delete them, crash occurs(obviously!).. so when u say child "detaches" itself from parent when goin out of scope..wont that occur in other cases too and parent wont try to delete ths child? and why the recommendation(in multiple threads) that these things SHOULD be created on stack..how and when(in what cases) would a child detach from parent? my main concern is how would a parent know the child has already gone out of scope and not try to delete it?
another thing thats done here is dialog on heap is given a parent and then deleted in the same function..wont the parent try to delete the dialog object or dialog again detaches?i mean once u give a widget a parent, deleting it yourself should be kinda foolish? i can go on with queriesbut essence of my question is how does a parent know which child to delete and in what cases would a child detach itself from parent?
This is really an academic talk. One allocation more or less doesn't make a difference, Qt doesn't use exceptions so only your code can be throwing one so you should know how/where to catch it, etc. These two are practically equivalent, the stack based is simpler to type in which is completely meaningless as well. There is a potential situation when a heap based dialogue would lead to a memory leak but then read my post about "enlightened users"![]()
Bookmarks