Results 1 to 3 of 3

Thread: Performance comparison of QDialog on stack and heap

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Performance comparison of QDialog on stack and heap

    Hi everyone,

    I read some articles about memory allocation and free in Qt recently. And I have question about the performance of creating QDialogs on stack (non-pointer) vs on heap (new).

    For stack, we just recreate the dialog every time and it should be auto-deleted after we close it, like
    Qt Code:
    1. void parentObjectClass::onButtonClick(){
    2. QDialog dialog(this);
    3. if(dialog.exe()){
    4. // do something
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    I think this one will auto delete the dialog instance every time after the dialog is closed. But every time the dialog need to be re-initialized.

    For heap, we pre-initialize the object and reuse it every time, like

    Qt Code:
    1. // header
    2. Class parentObjectClass{
    3. //....
    4. QDialog *dialog;
    5. //...
    6. };
    7.  
    8. // cpp
    9. void parentObjectClass::initialize(){
    10. dialog = new QDialog(this);
    11. }
    12.  
    13. void parentObjectClass::onButtonClick(){
    14. dialog->setDialog(values);
    15. if(dialog->exe()){
    16. // do something
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    This one just reset the dialog contents every time, but if we have a lot of dialogs, we have to initialize all of them when parentObjectClass instance is created, and they will occupy memory until parentObject is deleted.

    Am I right? And which way is better for practical using?
    Thanks.
    Last edited by Seishin; 22nd August 2012 at 15:54.

Similar Threads

  1. Allocating widgets on the stack verses using the heap
    By Ronayn in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2011, 00:14
  2. Replies: 3
    Last Post: 12th February 2011, 13:07
  3. Replies: 5
    Last Post: 29th November 2009, 15:09
  4. 2 questions: QAuthenticator and stack / heap
    By Tito Serenti in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2009, 06:56
  5. stack, heap and C#
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th August 2007, 18:40

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.