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] Am I deleating these variables right?
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Am I deleating these variables right?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 7.1k Views 4 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.
  • JohanSoloJ JohanSolo

    As far as I know, the Layout will not delete objects

    The layout won't, but the Window will.

    Also if you use window for checkbox
    checkbox *box = new checkbox(Window);
    It will be owned by window and deleted with it.

    A widget put in a layout which is then used in another widget has its parent reassigned. Therefore the QCheckBox will indeed be deleted at the time the Window is. I consider a good practice to set the parent anyway, I don't think it hurts.

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

    @JohanSolo
    Ahh, I did not know using a layout would reparent.

    So in this case, all he needs is
    delete window;

    JohanSoloJ 1 Reply Last reply
    0
    • mrjjM mrjj

      @JohanSolo
      Ahh, I did not know using a layout would reparent.

      So in this case, all he needs is
      delete window;

      JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #5

      @mrjj said:

      So in this case, all he needs is
      delete window;

      I think so, yes. This reparenting is explaing in the doc (Qt 5.5) or there for Qt 4.8. Look at the `Tips for Using Layouts' section.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #6

        Thanks for the replies, i'll check out those docs

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #7

          but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?

          also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?

          JohanSoloJ 1 Reply Last reply
          0
          • ? A Former User

            but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?

            also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?

            JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #8

            @manny said:

            but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?

            All the objects (deriving from QObject) you allocate and add to the layout of a widget will be destroyed when the widget itself is destroyed.

            @manny said:

            also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?

            If I'm not mistaken, you can rely on QApplication::aboutToQuit() to destroy your widget:

            connect( &a, SIGNAL( aboutToQuit() ), window, SLOT( deleteLater() ) );
            

            See the doc for detailed information.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #9

              so basically if I make a new instance of a class like

              ' className object = new className;
              object->show();

              it will be deleted once className is desetroyed

              JohanSoloJ 1 Reply Last reply
              0
              • ? A Former User

                so basically if I make a new instance of a class like

                ' className object = new className;
                object->show();

                it will be deleted once className is desetroyed

                JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on last edited by
                #10

                @manny said:

                so basically if I make a new instance of a class like

                ' className object = new className;
                object->show();

                it will be deleted once className is desetroyed

                Not exactly: every QWidget destroys its children. The deleteLater trick allows to destroy the top-level widget when the application quits.

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #11

                  ok, i think i understand now. Thanks for the help, really appreciate it.

                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    Although the answers given here were correct it is worth noting that the whole topic could have been avoided by not allocation the window on the heap. If the app has a single main widget it's simpler to just allocate it on the stack and don't fiddle with aboutToQuit:

                    int main(int argc, char *argv[])
                    {
                       QApplication a(argc, argv);
                    
                       QWidget window;
                       window.setWindowTitle("test");
                       window.show();
                    
                       return a.exec();
                    }
                    

                    Also, if a widget is created on the heap and you want it deleted when it is closed, the simplest way is to set appropriate attribute:

                    QWidget* w= new QWidget();
                    w->setAttribute(Qt::WA_DeleteOnClose); //this will call deleteLater() when widget is closed.
                    w->show();
                    
                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #13

                      thanks for the reply, will note that down

                      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