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. how to detect if a widget is destroyed?
Forum Updated to NodeBB v4.3 + New Features

how to detect if a widget is destroyed?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.1k Views 3 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.
  • Atr0p0sA Offline
    Atr0p0sA Offline
    Atr0p0s
    wrote on last edited by
    #1

    Hello everyone again!

    I faced a such situation:
    I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (used B->setAttribute(Qt::WA_DeleteOnClose);). But I also want widget B to destroy when A is closed.
    I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:

    void A::closeEvent(QCloseEvent* event)
    {
        if (p_B) {
           delete(p_B);
        }
        p_B = nullptr;
    }
    

    Can I get out of this situation preferably without changing widget B?

    JonBJ Chris KawaC jeremy_kJ 3 Replies Last reply
    0
    • Atr0p0sA Atr0p0s

      Hello everyone again!

      I faced a such situation:
      I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (used B->setAttribute(Qt::WA_DeleteOnClose);). But I also want widget B to destroy when A is closed.
      I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:

      void A::closeEvent(QCloseEvent* event)
      {
          if (p_B) {
             delete(p_B);
          }
          p_B = nullptr;
      }
      

      Can I get out of this situation preferably without changing widget B?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @Atr0p0s You can simply make B a child of A, i.e. pass A as a parent to B constructor or use the setParent method. A will delete its child when it is deleted itself.
      If you prefer to do it manually for some reason use QPointer instead of a raw pointer. It is automatically nulled when the object it points to is deleted.

      Atr0p0sA 1 Reply Last reply
      2
      • Atr0p0sA Atr0p0s

        Hello everyone again!

        I faced a such situation:
        I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (used B->setAttribute(Qt::WA_DeleteOnClose);). But I also want widget B to destroy when A is closed.
        I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:

        void A::closeEvent(QCloseEvent* event)
        {
            if (p_B) {
               delete(p_B);
            }
            p_B = nullptr;
        }
        

        Can I get out of this situation preferably without changing widget B?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Atr0p0s
        void QObject::destroyed(QObject *obj = nullptr).

        Use deleteLater() rather than delete.

        Don't do any of this stuff, just use parentage (assuming you can here): child widget B should have A as its parent.

        You shouldn't be in a complex situation like this, nobody else is.

        Atr0p0sA 1 Reply Last reply
        1
        • Atr0p0sA Atr0p0s

          Hello everyone again!

          I faced a such situation:
          I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (used B->setAttribute(Qt::WA_DeleteOnClose);). But I also want widget B to destroy when A is closed.
          I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:

          void A::closeEvent(QCloseEvent* event)
          {
              if (p_B) {
                 delete(p_B);
              }
              p_B = nullptr;
          }
          

          Can I get out of this situation preferably without changing widget B?

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @Atr0p0s You can simply make B a child of A, i.e. pass A as a parent to B constructor or use the setParent method. A will delete its child when it is deleted itself.
          If you prefer to do it manually for some reason use QPointer instead of a raw pointer. It is automatically nulled when the object it points to is deleted.

          Atr0p0sA 1 Reply Last reply
          2
          • Atr0p0sA Atr0p0s

            Hello everyone again!

            I faced a such situation:
            I have widget A that contains a pointer to child widget B and I want widget A to hide when X is clicked (it's by default), and widget B to destroy when its X is clicked (used B->setAttribute(Qt::WA_DeleteOnClose);). But I also want widget B to destroy when A is closed.
            I can't use the pointer to B, because it can be already destroyed if widget B is closed(destroyed) by the user:

            void A::closeEvent(QCloseEvent* event)
            {
                if (p_B) {
                   delete(p_B);
                }
                p_B = nullptr;
            }
            

            Can I get out of this situation preferably without changing widget B?

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @Atr0p0s said in how to detect if a widget is destroyed?:

                if (p_B) {
                   delete(p_B);
            

            On the pedantic side, testing for a nullptr prior to deleting isn't generally necessary.
            https://en.cppreference.com/w/cpp/language/delete:

            If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            Atr0p0sA 1 Reply Last reply
            1
            • JonBJ JonB

              @Atr0p0s
              void QObject::destroyed(QObject *obj = nullptr).

              Use deleteLater() rather than delete.

              Don't do any of this stuff, just use parentage (assuming you can here): child widget B should have A as its parent.

              You shouldn't be in a complex situation like this, nobody else is.

              Atr0p0sA Offline
              Atr0p0sA Offline
              Atr0p0s
              wrote on last edited by Atr0p0s
              #5

              @JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).
              And thank you again! I didn't know about deleteLater() :)

              JonBJ 1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                @Atr0p0s You can simply make B a child of A, i.e. pass A as a parent to B constructor or use the setParent method. A will delete its child when it is deleted itself.
                If you prefer to do it manually for some reason use QPointer instead of a raw pointer. It is automatically nulled when the object it points to is deleted.

                Atr0p0sA Offline
                Atr0p0sA Offline
                Atr0p0s
                wrote on last edited by Atr0p0s
                #6

                @Chris-Kawa Thank you! QPointer does exactly what I want.

                1 Reply Last reply
                0
                • Atr0p0sA Atr0p0s has marked this topic as solved on
                • jeremy_kJ jeremy_k

                  @Atr0p0s said in how to detect if a widget is destroyed?:

                      if (p_B) {
                         delete(p_B);
                  

                  On the pedantic side, testing for a nullptr prior to deleting isn't generally necessary.
                  https://en.cppreference.com/w/cpp/language/delete:

                  If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.

                  Atr0p0sA Offline
                  Atr0p0sA Offline
                  Atr0p0s
                  wrote on last edited by
                  #7

                  @jeremy_k Thanks, you are right! I was just trying somehow to get out of the situation :)

                  1 Reply Last reply
                  0
                  • Atr0p0sA Atr0p0s

                    @JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).
                    And thank you again! I didn't know about deleteLater() :)

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @Atr0p0s said in how to detect if a widget is destroyed?:

                    @JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).

                    So you could use the void QObject::destroyed(QObject *obj = nullptr) I referenced you to for detecting if B gets destroyed and then set A.p_B to nullptr. Which I suspect is just what QPointer does.

                    Atr0p0sA 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Atr0p0s said in how to detect if a widget is destroyed?:

                      @JonB parentage isn't enough because I have to delete widget B when A is hidden (closed).

                      So you could use the void QObject::destroyed(QObject *obj = nullptr) I referenced you to for detecting if B gets destroyed and then set A.p_B to nullptr. Which I suspect is just what QPointer does.

                      Atr0p0sA Offline
                      Atr0p0sA Offline
                      Atr0p0s
                      wrote on last edited by
                      #9

                      @JonB I got it. Much appreciated, sir!

                      1 Reply Last reply
                      0

                      • Login

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