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. Qt examples and the usage of pointers
Qt 6.11 is out! See what's new in the release blog

Qt examples and the usage of pointers

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 4.7k 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.
  • D Defohin

    What about the other things such as new QLineEdit is it going to be deleted automatically also? How do I know when a pointer is going to leak or not in Qt?

    p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by p3c0
    #4

    @Defohin AFAIK they are reparented. You see these widgets are added into the gridlayout and is then set as the main layout for EchoWindow. So when EchoWindow is deleted it deletes the layout which eventually deletes these widgets.

    157

    D 1 Reply Last reply
    1
    • p3c0P p3c0

      @Defohin AFAIK they are reparented. You see these widgets are added into the gridlayout and is then set as the main layout for EchoWindow. So when EchoWindow is deleted it deletes the layout which eventually deletes these widgets.

      D Offline
      D Offline
      Defohin
      wrote on last edited by
      #5

      @p3c0 How do I know when a pointer is going to leak or not in Qt?

      mrjjM 1 Reply Last reply
      0
      • D Defohin

        @p3c0 How do I know when a pointer is going to leak or not in Qt?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @Defohin said in Qt examples and the usage of pointers:

        @p3c0 How do I know when a pointer is going to leak or not in Qt?

        You cannot. Just like any other pointer.
        But you can Design your app so all Widgets
        have a Parent and all parents are owned by
        Main Window.
        So just normal care is needed.

        D 1 Reply Last reply
        0
        • mrjjM mrjj

          @Defohin said in Qt examples and the usage of pointers:

          @p3c0 How do I know when a pointer is going to leak or not in Qt?

          You cannot. Just like any other pointer.
          But you can Design your app so all Widgets
          have a Parent and all parents are owned by
          Main Window.
          So just normal care is needed.

          D Offline
          D Offline
          Defohin
          wrote on last edited by
          #7

          @mrjj That is kinda tricky, I tried to use a few softwares to detect leaking but they say that even if my pointer has a parent like new SomeClass(this) it's "leaking", it's probably false positive, but they say it's a leak, so I have no idea when it's an actual leak or not.

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

            Yes its tricky.
            Tools like valgrind needs tweaking to reduce the noise.
            If the tool is too simple , it dont understand the delete by parent and
            when it see no delete for it, flag as leak.

            On the bright side, its not that easy to make a leak as not assigning a parent will make it a
            window and embedding a widget into something will (almost) surely own it. ( like layouts)

            D 1 Reply Last reply
            0
            • mrjjM mrjj

              Yes its tricky.
              Tools like valgrind needs tweaking to reduce the noise.
              If the tool is too simple , it dont understand the delete by parent and
              when it see no delete for it, flag as leak.

              On the bright side, its not that easy to make a leak as not assigning a parent will make it a
              window and embedding a widget into something will (almost) surely own it. ( like layouts)

              D Offline
              D Offline
              Defohin
              wrote on last edited by
              #9

              @mrjj said in Qt examples and the usage of pointers:

              On the bright side, its not that easy to make a leak as not assigning a parent will make it a
              window and embedding a widget into something will (almost) surely own it. ( like layouts)

              What you mean with that? Sorry for my ignorance.

              mrjjM 1 Reply Last reply
              0
              • D Defohin

                @mrjj said in Qt examples and the usage of pointers:

                On the bright side, its not that easy to make a leak as not assigning a parent will make it a
                window and embedding a widget into something will (almost) surely own it. ( like layouts)

                What you mean with that? Sorry for my ignorance.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @Defohin
                Hi
                Well when I first started Qt I was also a bit sceptical about never
                calling delete on my widgets. ( as in pure c++ its a leak)

                However, if you try to design an application and forget to assign parent, you will find it as it becomes a window.

                QLabel *mine= new QLabel();
                mine->show()

                Will popup as a window and its very clear something its up.

                Also then you want to place it into some other widget, you
                use layouts.
                and as soon as you go
                somewidget->layout()->addWidget(mine)
                its owned by the layout.

                So its actually a bit hard to create a widget you dont want as window and create leak
                without assigning a parent as it will clearly show :)

                That leaves mostly QDialogs and windows to leak but there you have
                diag->setAttribute(Qt::WA_DeleteOnClose);
                So if closed, they clean up.

                D 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Defohin
                  Hi
                  Well when I first started Qt I was also a bit sceptical about never
                  calling delete on my widgets. ( as in pure c++ its a leak)

                  However, if you try to design an application and forget to assign parent, you will find it as it becomes a window.

                  QLabel *mine= new QLabel();
                  mine->show()

                  Will popup as a window and its very clear something its up.

                  Also then you want to place it into some other widget, you
                  use layouts.
                  and as soon as you go
                  somewidget->layout()->addWidget(mine)
                  its owned by the layout.

                  So its actually a bit hard to create a widget you dont want as window and create leak
                  without assigning a parent as it will clearly show :)

                  That leaves mostly QDialogs and windows to leak but there you have
                  diag->setAttribute(Qt::WA_DeleteOnClose);
                  So if closed, they clean up.

                  D Offline
                  D Offline
                  Defohin
                  wrote on last edited by
                  #11

                  @mrjj The same happens with QObject? If I don't assign a parent to it?

                  mrjjM 1 Reply Last reply
                  0
                  • D Defohin

                    @mrjj The same happens with QObject? If I don't assign a parent to it?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #12

                    Yes, its actually QObject that does this Parent->Child deleting and not Qwidgets :)

                    But QObject dont become window. Its a QWidgets thing.

                    Just so we are clear. All QWidgets are QObject :)

                    D 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Yes, its actually QObject that does this Parent->Child deleting and not Qwidgets :)

                      But QObject dont become window. Its a QWidgets thing.

                      Just so we are clear. All QWidgets are QObject :)

                      D Offline
                      D Offline
                      Defohin
                      wrote on last edited by
                      #13

                      @mrjj Meaning that if I don't delete QObject or assign a parent to it, it's going to leak, correct?

                      mrjjM 1 Reply Last reply
                      0
                      • D Defohin

                        @mrjj Meaning that if I don't delete QObject or assign a parent to it, it's going to leak, correct?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @Defohin
                        Yes that could happen.
                        http://doc.qt.io/qt-5/objecttrees.html

                        D 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @Defohin
                          Yes that could happen.
                          http://doc.qt.io/qt-5/objecttrees.html

                          D Offline
                          D Offline
                          Defohin
                          wrote on last edited by
                          #15

                          @mrjj Thank you, it's really tricky, but I'll try to be careful with that.

                          mrjjM 1 Reply Last reply
                          0
                          • D Defohin

                            @mrjj Thank you, it's really tricky, but I'll try to be careful with that.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #16

                            @Defohin
                            well, you should also look into using smart pointers.
                            Thats how its managed in plain c++.
                            http://stackoverflow.com/questions/31750689/how-to-use-smart-pointer-for-auto-clean-up
                            https://en.wikipedia.org/wiki/Smart_pointer

                            Small note. Do not put smart pointers on Widgets with parents as it get ugly :)

                            Also Qt comes with Qt versions of those.

                            D 1 Reply Last reply
                            2
                            • mrjjM mrjj

                              @Defohin
                              well, you should also look into using smart pointers.
                              Thats how its managed in plain c++.
                              http://stackoverflow.com/questions/31750689/how-to-use-smart-pointer-for-auto-clean-up
                              https://en.wikipedia.org/wiki/Smart_pointer

                              Small note. Do not put smart pointers on Widgets with parents as it get ugly :)

                              Also Qt comes with Qt versions of those.

                              D Offline
                              D Offline
                              Defohin
                              wrote on last edited by
                              #17

                              @mrjj Thank you very much.

                              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