Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating a QDialog per new as a pointer vs. initializing it directly

Creating a QDialog per new as a pointer vs. initializing it directly

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 738 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by
    #1

    Hi all,

    I'd be very grateful for some advice/clarification on how to use a QDialog derived class correctly. I can show such a class in two ways: either, I do

    SomeDialog *dialog = new SomeDialog(whatever);
    dialog->show();
    

    or I can do

    SomeDialog dialog(whatever);
    dialog.show();
    

    Is it correct that, if I do setAttribute(Qt::WA_DeleteOnClose); for the first variant, the result will be the very same, as the pointer will be deleted when the dialog exists, and the variable of the second one will also be deleted as soon as it goes out of scope (the function creating the dialog exits)?

    If so, can one say that one sould always use the second, pointer-less solution if no connect() statement is used and the dialog won't be used later (as it's deleted anyway on exiting)?

    And: Is it possible to know if such a dialog is created via new or directly, so that if I need both variants, setAttribute(Qt::WA_DeleteOnClose); can be set automatically, and only for the new variant, so that I don't get a double free or corruption error if it's used directly?

    Thanks a lot for all clarification/help!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      SomeDialog dialog(whatever);
      dialog.show();
      would only work in main.cpp as exec() blocks it from running out of scope

      if you do it in a function like

      void some func()  {
      SomeDialog dialog(whatever);
      dialog.show(); // not blocking
      } // SomeDialog would be deleted  here 
      

      for this to work u have to use exec() which is blocking.

      and yes, setAttribute(Qt::WA_DeleteOnClose); will delete the dialog when it is closed.
      for the pointer version.

      so if you use show() use pointer and WA_DeleteOnClose for auto clean up.
      if you use exec()
      no need to use new.

      l3u_L 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        SomeDialog dialog(whatever);
        dialog.show();
        would only work in main.cpp as exec() blocks it from running out of scope

        if you do it in a function like

        void some func()  {
        SomeDialog dialog(whatever);
        dialog.show(); // not blocking
        } // SomeDialog would be deleted  here 
        

        for this to work u have to use exec() which is blocking.

        and yes, setAttribute(Qt::WA_DeleteOnClose); will delete the dialog when it is closed.
        for the pointer version.

        so if you use show() use pointer and WA_DeleteOnClose for auto clean up.
        if you use exec()
        no need to use new.

        l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by
        #3

        @mrjj Oh, that's another aspect of this I didn't think of yet … thanks for the help :-)

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved