Results 1 to 20 of 20

Thread: Two ways, Which one is better?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17

    Default Two ways, Which one is better?

    Hello! Long time no see!

    The following two fragments, which is better, why?

    Qt Code:
    1. void oneDialog::function()
    2. {
    3. QTestDialog* pt= new QTestDialog(this);
    4. // ........
    5. delete pt;
    6. }
    7.  
    8.  
    9.  
    10. void oneDialog::function()
    11. {
    12. QTestDialog dialog(this);
    13. // ........
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 


    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Two ways, Which one is 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 66 Times in 62 Posts

    Default Re: Two ways, Which one is 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..

  4. #4
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    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..

    You mean that the dialog should be a modal one?

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 66 Times in 62 Posts

    Default Re: Two ways, Which one is better?

    no, that wont make difference..all i mean is widgets and dialogs should always be created on heap..AFAIK

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 66 Times in 62 Posts

    Default Re: Two ways, Which one is better?

    so will that concept apply even in the case of a normal dialog(non-modal)?

  8. #8
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: Two ways, Which one is better?

    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...

  9. #9
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17

    Default Re: Two ways, Which one is better?

    Does this forum has any utilities to save a thread, such add to favouites?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    so will that concept apply even in the case of a normal dialog(non-modal)?
    No. A non-modal dialog has to be created on heap.

    Quote Originally Posted by HelloDan View Post
    Does this forum has any utilities to save a thread, such add to favouites?
    Yeah, there is a "Bookmarks" table at the bottom of the page.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 66 Times in 62 Posts

    Default Re: Two ways, Which one is better?

    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.

  12. #12
    Join Date
    May 2008
    Posts
    155
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanked 15 Times in 13 Posts

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by wysota View Post
    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.
    It also use one allocation less and has a slightly better chance to be exception save. I can't think of any situation where the first form is better.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    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"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Is there any ways to check for nan inf and -inf in Qt?
    By aarelovich in forum Qt Programming
    Replies: 5
    Last Post: 2nd June 2016, 13:29
  2. Replies: 2
    Last Post: 16th July 2010, 16:03
  3. Model-view: Display items in different ways
    By taboom in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2007, 20:05
  4. ways to exit appl.
    By whoops.slo in forum Newbie
    Replies: 1
    Last Post: 29th January 2007, 19:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.