Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am relatively new to the Qt framework and I was wondering if I should delete pointers in my program. I know that, in C++, if memory is not return it could lead to memory leak, but I am not sure if the same applies to Qt.

#include "mainwindow.h"
#include <QApplication>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QIcon>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget *w = new  QWidget();
    w->setWindowTitle("Music Player");
    QIcon * mainwind_icon = new QIcon("MusicPlayer.png");
    w->setWindowIcon(*mainwind_icon);

    QPushButton * enter_button = new QPushButton();
    QTextEdit  * textbox = new QTextEdit();

    QHBoxLayout * vlayout = new QHBoxLayout;
    vlayout->addWidget(textbox);
    vlayout->addWidget(enter_button);

    w->setLayout(vlayout);




    w -> show();

    return a.exec();
}
share|improve this question
1  
ratchet freak's answer is more relevant (and correct) to your specific Qt question. You don't need to delete anything manually if it's parented. –  Sosukodo Mar 2 at 6:08
1  
@user3314865 Please unaccept my answer. Seems I stuck my foot in my mouth by making assumptions about Qt that proved to be very wrong. I'm sorry I've mislead you, and I hope it's not too late for the damage to be undone. –  Corbin Mar 2 at 20:03
    
Its fine don't worry about it –  user3314865 Mar 2 at 20:56
    
@Corbin Your answer still applied to pointers that are independent of Qt framework so I still learned something new –  user3314865 Mar 2 at 21:01
add comment

1 Answer 1

up vote 6 down vote accepted

in Qt there is a concept of parents and a hierarchy

in essence it means that when a qobject owns another qobject (the other qobject's parent is the first) then the parent will take care of cleaning it's children

there are 2 ways an object becomes a child of another: when it is set in the constructor or when reparented. the qwidget family automatically reparents widgets which you add to it

common use for the main function is to allocate the root statically and all the rest dynamically as child of the root:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w ();
    w.setWindowTitle("Music Player");
    QIcon * mainwind_icon = new QIcon("MusicPlayer.png");
    w.setWindowIcon(*mainwind_icon);

    QPushButton * enter_button = new QPushButton();
    QTextEdit  * textbox = new QTextEdit();

    QHBoxLayout * vlayout = new QHBoxLayout;
    vlayout->addWidget(textbox);
    vlayout->addWidget(enter_button);

    w.setLayout(vlayout);

    w.show();
    return a.exec();
}

the setlayout takes ownership of the vlayout and its children so the destructor of w will delete those as well

when you are inside an event loop then you can delete a qobject by calling deleteLater on it which will schedule the object (and its children) for deletion. you can also connect a signal to it if needed

share|improve this answer
    
+1 for discussing ownership. Understanding ownership of resources is critical to using any library. –  Michael Urman Mar 2 at 14:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.