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. QDialog attribute WA_DeleteOnClose
Forum Updated to NodeBB v4.3 + New Features

QDialog attribute WA_DeleteOnClose

Scheduled Pinned Locked Moved Solved General and Desktop
qdialog
9 Posts 4 Posters 12.9k Views 1 Watching
  • 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.
  • Gianluca86G Offline
    Gianluca86G Offline
    Gianluca86
    wrote on last edited by
    #1

    Hi,
    In the main window, I call the MyDialog class that derives from QDialog class:

    // in MainWindow
    MyDialog *d = new MyDialog();
    d->setAttribute(Qt::WA_DeleteOnClose);
    d->show();
    

    By WA_DeleteOnClose attribute, I can delete MyDialog window, but the "d" pointer remains valid. How do I make sure that when I close the MyDialog the "d" pointer is deallocated?

    Thanks

    M 1 Reply Last reply
    0
    • Gianluca86G Gianluca86

      Hi,
      In the main window, I call the MyDialog class that derives from QDialog class:

      // in MainWindow
      MyDialog *d = new MyDialog();
      d->setAttribute(Qt::WA_DeleteOnClose);
      d->show();
      

      By WA_DeleteOnClose attribute, I can delete MyDialog window, but the "d" pointer remains valid. How do I make sure that when I close the MyDialog the "d" pointer is deallocated?

      Thanks

      M Offline
      M Offline
      Max13
      wrote on last edited by
      #2

      @Gianluca86 Hi there! Qt::WA_DeleteOnClose is made to delete the pointer when the widget is closed. If you manually delete d;, the pointer won't be valid anymore

      We all have started by asking questions. Then after some time, we can begin answering them.

      1 Reply Last reply
      0
      • Gianluca86G Offline
        Gianluca86G Offline
        Gianluca86
        wrote on last edited by
        #3

        Ok, I try to be more specific.

        // in MainWindow
        if(d == NULL)
        {
           MyDialog *d = new MyDialog();
           d->setAttribute(Qt::WA_DeleteOnClose);
           d->DoSomething();
           d->show();
        }
        else
        {
          d->DoSomething();
        }
        

        DoSomething() is a public function within MyDialog.
        When d is NULL means that I have not opened the window, so I create, otherwise I assume that the window is already open, and then only the active DoSomething() function. But if I close d is not NULL but obviously goes wrong just meets the line d->DoSomething().

        I hope everything is clear.

        jsulmJ 2 Replies Last reply
        0
        • Gianluca86G Gianluca86

          Ok, I try to be more specific.

          // in MainWindow
          if(d == NULL)
          {
             MyDialog *d = new MyDialog();
             d->setAttribute(Qt::WA_DeleteOnClose);
             d->DoSomething();
             d->show();
          }
          else
          {
            d->DoSomething();
          }
          

          DoSomething() is a public function within MyDialog.
          When d is NULL means that I have not opened the window, so I create, otherwise I assume that the window is already open, and then only the active DoSomething() function. But if I close d is not NULL but obviously goes wrong just meets the line d->DoSomething().

          I hope everything is clear.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Gianluca86 Your dialog will be deleted when it is closed, but the pointer to it will not be changed. Why should it be changed and by whom? The dialog itself does not know whether there are any pointers pointing to it. So nobody assigns NULL to it.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Gianluca86G Gianluca86

            Ok, I try to be more specific.

            // in MainWindow
            if(d == NULL)
            {
               MyDialog *d = new MyDialog();
               d->setAttribute(Qt::WA_DeleteOnClose);
               d->DoSomething();
               d->show();
            }
            else
            {
              d->DoSomething();
            }
            

            DoSomething() is a public function within MyDialog.
            When d is NULL means that I have not opened the window, so I create, otherwise I assume that the window is already open, and then only the active DoSomething() function. But if I close d is not NULL but obviously goes wrong just meets the line d->DoSomething().

            I hope everything is clear.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @Gianluca86 One more comment: when dialog is deleted the pointer still contains the address but is NOT valid anymore (that's why your application would crash if you would try to use that pointer).

            int *p = new int();
            // p is a valid pointer
            delete p;
            // p still has the old value (the address) but is not valid any-more
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Gianluca86G Offline
              Gianluca86G Offline
              Gianluca86
              wrote on last edited by
              #6

              I know, I would like to assign NULL when the window is closed, but I would not use the new signals.
              For example, if the destroyer of Dialog emit a signal to the main window I can asegnare the NULL pointer. I just wanted to avoid this, but I believe there is no alternative.

              jsulmJ kshegunovK 2 Replies Last reply
              0
              • Gianluca86G Gianluca86

                I know, I would like to assign NULL when the window is closed, but I would not use the new signals.
                For example, if the destroyer of Dialog emit a signal to the main window I can asegnare the NULL pointer. I just wanted to avoid this, but I believe there is no alternative.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Gianluca86 You could use http://doc.qt.io/qt-5/qobject.html#destroyed. You only need to add a slot for this signal and connect both.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • Gianluca86G Gianluca86

                  I know, I would like to assign NULL when the window is closed, but I would not use the new signals.
                  For example, if the destroyer of Dialog emit a signal to the main window I can asegnare the NULL pointer. I just wanted to avoid this, but I believe there is no alternative.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @Gianluca86 said:

                  I know, I would like to assign NULL when the window is closed, but I would not use the new signals.

                  QPointer provides guarded pointers for QObjects. You can use it to hold a reference to your dialog and when the dialog is deleted the pointer will be set to NULL automatically.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • Gianluca86G Offline
                    Gianluca86G Offline
                    Gianluca86
                    wrote on last edited by
                    #9

                    Both solutions are great, now I see what to use.
                    Thank you all

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm referenced this topic on

                    • Login

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