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. I need to delete objects in mainwindow?

I need to delete objects in mainwindow?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 17.3k 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.
  • A Offline
    A Offline
    Arendell
    wrote on 11 Sept 2013, 21:31 last edited by
    #1

    @MainWindow::~MainWindow()
    {
    delete ui;
    }@

    This is default code when I create a project in Qt Creator. It's necessary? I think after close program OS will delete it automatically.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Sept 2013, 21:46 last edited by
      #2

      Hi,

      Yes it's necessary, ui is allocated on the heap, so it must be deleted when not used anymore. Otherwise you would have a memory leak.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Arendell
        wrote on 11 Sept 2013, 21:57 last edited by
        #3

        OK ui is necessary. How about QWidget object? Let's say it's created in MainWindow. Do I need to delete it on MainWindow's destructor?

        @MainWindow::~MainWindow()
        {
        delete QWidget1; // Need?
        delete QWidget2;// Need?
        delete QWidget3;// Need?
        delete ui;// OK
        }@

        OK ui is needed to be delete but how I know which object need to be delete?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on 12 Sept 2013, 06:30 last edited by
          #4

          All objects that you allocated on the heap and which don't have a QObject parent set have to be deleted by you.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Arendell
            wrote on 12 Sept 2013, 10:28 last edited by
            #5

            Hmm, OK.

            Thanks
            [quote author="KA51O" date="1378967418"]All objects that you allocated on the heap and which don't have a QObject parent set have to be deleted by you.[/quote]

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on 12 Sept 2013, 11:37 last edited by
              #6

              Just to clarify.
              @
              MainWindow: public QMainWindow
              {
              MainWindow(QObject* a_parent);
              ~MainWindow();

              QWidget m_StackWidget;
              QWidget* m_HeapWidgetWithParent;
              QWidget* m_HeapWidgetWithoutParent;
              NoQObjectDerivedClass m_StackNoQObj;
              NoQObjectDerivedClass* m_HeapNoQObj;
              };

              MainWindow::MainWindow(QObject* a_parent)
              {
              // needs to be deleted by you
              m_HeapWidgetWithoutParent = new QWidget();
              //doesn't need to be deleted by you
              m_HeapWidgetWithParent = new QWidget(this);
              // needs to be deleted by you
              m_HeapNoQObj = new NoQObjectDerivedClass();
              }

              MainWindow::~MainWindow()
              {
              delete m_HeapWidgetWithoutParent;
              delete m_HeapNoQObj;
              }
              @

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jeroentjehome
                wrote on 13 Sept 2013, 06:21 last edited by
                #7

                Hi,
                If this answered your question, set [SOLVED] in front of your first post! Saves other guys reading it.

                Greetz, Jeroen

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Blizzard365
                  wrote on 18 Dec 2017, 00:10 last edited by
                  #8

                  Hi,
                  Do you figure out that? I have the same question. Can you help me with that?

                  J 1 Reply Last reply 18 Dec 2017, 05:32
                  0
                  • B Blizzard365
                    18 Dec 2017, 00:10

                    Hi,
                    Do you figure out that? I have the same question. Can you help me with that?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 18 Dec 2017, 05:32 last edited by
                    #9

                    @Blizzard365 What question?
                    In C++ you have to free unused memory by yourself.
                    In Qt you don't have to delete children (widgets for example) which have parents - when parent is deleted the children are deleted automatically. See http://doc.qt.io/qt-5/objecttrees.html

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    B 1 Reply Last reply 19 Dec 2017, 08:40
                    4
                    • J jsulm
                      18 Dec 2017, 05:32

                      @Blizzard365 What question?
                      In C++ you have to free unused memory by yourself.
                      In Qt you don't have to delete children (widgets for example) which have parents - when parent is deleted the children are deleted automatically. See http://doc.qt.io/qt-5/objecttrees.html

                      B Offline
                      B Offline
                      Blizzard365
                      wrote on 19 Dec 2017, 08:40 last edited by
                      #10

                      @jsulm thx
                      I got it, ui is not a QObject. I have mistaken 'ui' as a QObject, and setUpUi(MainWindow) is setting parent for 'ui', which is unappropriate.

                      1 Reply Last reply
                      2

                      • Login

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