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.
  • A Offline
    A Offline
    Atr0p0s
    wrote on 1 May 2023, 19:03 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?

    J C J 3 Replies Last reply 1 May 2023, 19:07
    0
    • A Atr0p0s
      1 May 2023, 19:03

      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?

      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 1 May 2023, 19:09 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.

      A 1 Reply Last reply 2 May 2023, 09:18
      2
      • A Atr0p0s
        1 May 2023, 19:03

        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?

        J Offline
        J Offline
        JonB
        wrote on 1 May 2023, 19:07 last edited by JonB 5 Jan 2023, 19:09
        #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.

        A 1 Reply Last reply 2 May 2023, 09:17
        1
        • A Atr0p0s
          1 May 2023, 19:03

          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?

          C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 1 May 2023, 19:09 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.

          A 1 Reply Last reply 2 May 2023, 09:18
          2
          • A Atr0p0s
            1 May 2023, 19:03

            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?

            J Offline
            J Offline
            jeremy_k
            wrote on 2 May 2023, 05:08 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/

            A 1 Reply Last reply 2 May 2023, 09:26
            1
            • J JonB
              1 May 2023, 19:07

              @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.

              A Offline
              A Offline
              Atr0p0s
              wrote on 2 May 2023, 09:17 last edited by Atr0p0s 5 Feb 2023, 09:19
              #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() :)

              J 1 Reply Last reply 2 May 2023, 09:28
              0
              • C Chris Kawa
                1 May 2023, 19:09

                @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.

                A Offline
                A Offline
                Atr0p0s
                wrote on 2 May 2023, 09:18 last edited by Atr0p0s 5 Feb 2023, 09:19
                #6

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

                1 Reply Last reply
                0
                • A Atr0p0s has marked this topic as solved on 2 May 2023, 09:18
                • J jeremy_k
                  2 May 2023, 05:08

                  @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.

                  A Offline
                  A Offline
                  Atr0p0s
                  wrote on 2 May 2023, 09:26 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
                  • A Atr0p0s
                    2 May 2023, 09:17

                    @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() :)

                    J Offline
                    J Offline
                    JonB
                    wrote on 2 May 2023, 09:28 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.

                    A 1 Reply Last reply 2 May 2023, 11:48
                    1
                    • J JonB
                      2 May 2023, 09:28

                      @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.

                      A Offline
                      A Offline
                      Atr0p0s
                      wrote on 2 May 2023, 11:48 last edited by
                      #9

                      @JonB I got it. Much appreciated, sir!

                      1 Reply Last reply
                      0

                      1/9

                      1 May 2023, 19:03

                      • Login

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