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. [solved!]What's the proper way to delete/null a pointer to another dialog that has closed?
Forum Updated to NodeBB v4.3 + New Features

[solved!]What's the proper way to delete/null a pointer to another dialog that has closed?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 7.2k 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.
  • P Offline
    P Offline
    pwnstar23
    wrote on 18 Sept 2012, 16:57 last edited by
    #1

    So I have a mainwindow class, all it has right now is a button. This button opens another dialog, and sets a member pointer in mainwindow to that new dialog. The second dialog has a button that sends a signal to close itself. So the window is gone but my pointer in mainwindow is still pointing to it, it doesn't know it should delete/null the pointer.

    @
    class AnotherSubDialog;
    class MainWindow
    {
    //stuff
    AnotherSubDialog* mpSubDialog; // this object will call close() when it's "close" button is clicked. However the mainwindow class is not aware of this and doesn't delete/null the pointer.
    };
    @

    What is the proper way to handle this behavior? I would image this kind of thing is done all the time.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      noah
      wrote on 18 Sept 2012, 17:25 last edited by
      #2

      You can have the dialog delete itself by setting the Qt::WA_DeleteOnClose attribute.
      You can use a QPointer instead of a standard pointer to have it clear itself when the memory its pointing to is deleted.

      Alternately, you can create a slot in your MainWindow and connect the mpSubDialog finished(int) signal to that slot, then take care of deleting the pointer there.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        twsimpson
        wrote on 18 Sept 2012, 17:37 last edited by
        #3

        You could always connect the signal emitted when your dialog closes to it's "deleteLater() slot": http://qt-project.org/doc/qt-4.8/qobject.html#deleteLater

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pwnstar23
          wrote on 18 Sept 2012, 18:15 last edited by
          #4

          [quote author="Noah" date="1347989118"]You can have the dialog delete itself by setting the Qt::WA_DeleteOnClose attribute.
          You can use a QPointer instead of a standard pointer to have it clear itself when the memory its pointing to is deleted.

          Alternately, you can create a slot in your MainWindow and connect the mpSubDialog finished(int) signal to that slot, then take care of deleting the pointer there.[/quote]

          I did the signal slot method you mentioned. When I had the slot delete the sub dialog pointer it would crash the app. When I only nulled the pointer it works fine. Is it crashing cause I am deleting the dialog for a second time? Meaning the sub dialog is deleting itself and any associated things I have added?

          1 Reply Last reply
          0
          • N Offline
            N Offline
            noah
            wrote on 18 Sept 2012, 18:26 last edited by
            #5

            [quote author="pwnstar23" date="1347992140"]
            I did the signal slot method you mentioned. When I had the slot delete the sub dialog pointer it would crash the app. When I only nulled the pointer it works fine. Is it crashing cause I am deleting the dialog for a second time? Meaning the sub dialog is deleting itself and any associated things I have added? [/quote]

            It's crashing because you're deleting the object emiting the signal. You should call mpSubDialog->deleteLater() instead, as Terence suggested.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 18 Sept 2012, 19:35 last edited by
              #6

              I think you are both right: you are deleting from a signal, but you are also deleting a second time. I recommend you use the QPointer method instead.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pwnstar23
                wrote on 18 Sept 2012, 21:41 last edited by
                #7

                If I use a QPointer then can I get away with just making the SubWindows finished signal, link to a custom slot that has deletelater() in it?

                @
                mpSubWindow = new SubWindow(this);
                connect( mpSubWindow, SIGNAL( finished(int) ), mpSubWindow, SLOT( DeleteSlot( int )));
                mpSubWindow->show();

                private slots:
                void DeleteSlot(int value)
                {
                this->deleteLater();
                }
                @

                Or is there a better place for the deleteLater() call to be located?

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  noah
                  wrote on 18 Sept 2012, 21:55 last edited by
                  #8

                  [quote author="pwnstar23" date="1348004498"]If I use a QPointer then can I get away with just making the SubWindows finished signal, link to a custom slot that has deletelater() in it?
                  [/quote]

                  You certainly can. In fact, you can connect it directly, since you don't need a perfect match in the parameters from the signal and slot you're connecting. Of course if there's anything else you want to do when the dialog closes your method is better.

                  @connect(mpSubWindow, SIGNAL(finished(int)), mpSubWindow, SLOT(deleteLater()))@

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on 19 Sept 2012, 06:04 last edited by
                    #9

                    Noah is right. Though, if there is anything else you would like to do on finish, you could also simply use two connects. I usually prefer that: have one function that does one thing, rather than one function that reacts to one signal.

                    Note that instead of using the deleteLater, you could also set the delete-on-close flag of the window and have Qt take care of deleting it. You do that by calling
                    @
                    mpSubWindow->setAttribute(Qt::WA_DeleteOnClose, true);
                    @
                    It doesn't matter which you use really.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pwnstar23
                      wrote on 23 Sept 2012, 01:09 last edited by
                      #10

                      [quote author="Andre" date="1348034642"]

                      Note that instead of using the deleteLater, you could also set the delete-on-close flag of the window and have Qt take care of deleting it. You do that by calling
                      @
                      mpSubWindow->setAttribute(Qt::WA_DeleteOnClose, true);
                      @
                      It doesn't matter which you use really.
                      [/quote]

                      This seems like the simplest, less error prone way of doing it after reading all the responses.

                      Thanks guys, this thread is now solved!

                      Note for future people: mpSubWindow is a QPointer<SubWindowClass>.

                      1 Reply Last reply
                      0

                      9/10

                      19 Sept 2012, 06:04

                      • Login

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