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]Should i Manully delete controls in the MainWindow's destructor?
QtWS25 Last Chance

[SOLVED]Should i Manully delete controls in the MainWindow's destructor?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.6k Views
  • 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by opengpu2
    #1

    i create the ui controls setting it's parent to the window?
    so should i delelte and set them to NULL in the window's destructor?
    or should i do this delete in the window's closeEvent() func?
    thank you!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      msms
      wrote on last edited by
      #2

      @opengpu2 said:

      i create the ui controls setting it's parent to the window?
      so should i delelte and set them to NULL in the window's destructor?
      or should i do this delete in the window's closeEvent() func?
      thank you!

      posted 22 minutes ago

      Neither. If the control has the window as its parent, the window will automatically take care of cleaning it up.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        GENERAL RUE: When an QObject is destroyed, it automatically destroys all its children.

        If you did something like

        MyClass::MyClass(QWidget* parent): QMainWindow(parent)
        {
        label = new QLabel(this)
        edit = new QLineEdit(this);
        ....
        }

        it works

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • O Offline
          O Offline
          opengpu2
          wrote on last edited by
          #4

          how about QGraphicsItem ?
          i notice that, when pItem was created, no parent was sent to it, and then just scene->addItem(pItem).
          what should i do when i close the widget which contain the QGraphicsView/Scene?
          should i delete all the QGraphicsItem in the QGraphicsView's destructor or in closeEvent?
          thank you

          1 Reply Last reply
          0
          • M Offline
            M Offline
            msms
            wrote on last edited by
            #5

            @opengpu2 said:

            how about QGraphicsItem ?

            Yes. If you addItem, the parent and ownership is set to the QGraphicsView/Scene. The documentation for these classes describes this. I suggest opening up Qt assistant and reading, the documentation is really terrific.

            1 Reply Last reply
            0
            • O Offline
              O Offline
              opengpu2
              wrote on last edited by
              #6

              thank you.
              so u mean, like this:
              pItem = new QGraphicsItem( 0 ); //no parent was send here
              scene->addItem(pItem);

              the parent is still the scene.
              and when i close the widget which owns the QGraphicsView/Scene.
              still no need to Manully delete the pItem, right?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                From Qt docs

                QGraphicsScene::addItem()
                Adds or moves the item and all its children to this scene. This scene takes ownership of the item.

                So, when a scene is destroyed, all items will be destroyed too

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                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