user1604443
user1604443

Reputation: 59

Delete created Qt elements and subclasses

I have a question about Qt. I am wondering how it is I should delete all the pointers I create. For example:

.h file

#ifndef MAINCALENDAR_H
#define MAINCALENDAR_H
#include<QWidget>
#include <QMap>
#include <QComboBox>
#include <QCalendarWidget>
#include <QRadioButton>
#include <QString>
#include <QtGui>
#include "selector.h"
#include <QInputDialog>

class mainCalendar : public QWidget
{
    Q_OBJECT
public:
    mainCalendar(QWidget * parent = 0);
    ~mainCalendar();
    void showAppointments();

public slots:
    void showLCFunc() {select->getTod()->getIntf()->getListClass().orderListChronologic(); printer * test = new printer; test->setList(TodFace->getList()); test->show();}
    void showLPFunc() {select->getTod()->getIntf()->getListClass().orderListByPriority(); printer * test = new printer; test->setList(TodFace->getList()); test->show();}
    void loadFile() {QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"", tr("M-Calendar Files (*.mca)"));}
    void saveFile() {QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"", tr("M-Calendar Files (*.mca)"));qDebug() << fileName << endl;}
    void intshow();
    void updater() {if (!Interface->isHidden()) { app->setList(Interface->getList()); app->Beta_update(1,calendar->selectedDate()); }
        else if (!TodFace->isHidden()) {tod->setList(TodFace->getList()); tod->Beta_update(1,calendar->selectedDate());} }

private:
    QPushButton *showLC;
    QPushButton *showLP;
    QPushButton *searchButton;
    QPushButton *updateButton;
    QPushButton *saveButton;
    QPushButton *loadButton;
    QLabel *instructions;
    QPushButton *backButton;
    QPushButton *taskButton;
    interface * Interface;
    todFace * TodFace;
    showTod * tod;
    showApp * app;
    QCalendarWidget *calendar;
    QGridLayout *mainLayout;
    Selector * select;
};

#endif // MAINCALENDAR_H

.cpp file:

#include "maincalendar.h"

mainCalendar::mainCalendar(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout * footButtons = new QHBoxLayout;
    showLC = new QPushButton(tr("'to-do' (chrono)"));
    showLP = new QPushButton(tr("'to-do' (priority)"));
    searchButton = new QPushButton(tr("&Search"));
    saveButton = new QPushButton(tr("&Save calendar"));
    loadButton = new QPushButton(tr("&Load Calendar"));
    updateButton = new QPushButton(tr("Update"));
    footButtons->addWidget(searchButton);
    footButtons->addWidget(saveButton);
    footButtons->addWidget(loadButton);
    footButtons->addWidget(showLC);
    footButtons->addWidget(showLP);
    instructions = new QLabel(tr("To view or add data, double-click date in calendar"));
    calendar = new QCalendarWidget;
    calendar->setGridVisible(true);
    calendar->setMinimumDate(QDate(2012, 1, 1));
    calendar->setMaximumDate(QDate(2016,12,31));
    backButton = new QPushButton(tr("&Back to calendar"));
    select = new Selector(0,calendar,instructions,backButton, updateButton);


    tod = select->getTod();
    TodFace = tod->getIntf();
    TodFace->hide();
    TodFace->setCalendar(calendar);
    tod->hide();
    app = select->getApp();
    Interface = app->getIntf();
    Interface->hide();
    Interface->setCalendar(calendar);
    app->hide();



    backButton->hide();
    updateButton->hide();
    connect(showLC,SIGNAL(clicked()),this,SLOT(showLCFunc()));
    connect(showLP,SIGNAL(clicked()),this,SLOT(showLPFunc()));
    connect(updateButton, SIGNAL(clicked()), this, SLOT(updater()));
    connect(backButton, SIGNAL(clicked()), this, SLOT(intshow()));
    connect(loadButton,SIGNAL(clicked()),this,SLOT(loadFile()));
    connect(saveButton,SIGNAL(clicked()),this,SLOT(saveFile()));

    connect(calendar, SIGNAL(activated(QDate)), this, SLOT(intshow()));
    mainLayout = new QGridLayout;
    this->setMinimumHeight(800);
    this->setMinimumWidth(1000);
    mainLayout->setColumnMinimumWidth(0,500);
    mainLayout->addWidget(calendar,0,0);
    mainLayout->addWidget(app,1,0);
    mainLayout->addWidget(Interface,1,2);
    mainLayout->addWidget(tod,1,0);
    mainLayout->addWidget(TodFace,1,2);
    mainLayout->addWidget(backButton,0,0,Qt::AlignTop);
    mainLayout->addLayout(footButtons,2,0,Qt::AlignLeading);
    mainLayout->addWidget(instructions,2,0,Qt::AlignTrailing);
    mainLayout->addWidget(updateButton,2,2,Qt::AlignRight);
    setLayout(mainLayout);

    setWindowTitle(tr("M-Calendar"));
}

mainCalendar::~mainCalendar()
{
}

void mainCalendar::intshow()
{
    if (Interface->isHidden()&&TodFace->isHidden())
    {
        select->setDate(calendar->selectedDate());
        select->show();
        Interface->setdate(calendar->selectedDate());
        TodFace->setdate(calendar->selectedDate());
    } else
    {
        backButton->hide();
        updateButton->hide();
        Interface->hide();
        app->close();
        TodFace->hide();
        tod->close();
        calendar->show();
        instructions->show();
    }
}

I am stuck here. Am I supposed to do to delete all the pointers (QPushbutton, etc) and subclasses so no memory leaks occur?

Upvotes: 0

Views: 307

Answers (2)

Bart
Bart

Reputation: 20028

Short answer: No, you don't have to explicitly delete them.

In Qt, the QObjects are organized in object trees. The parent-child relationships of the various widgets also implies that the parent takes ownership of the child widgets.

As a result you do not have to explicitly delete them when your application finishes. Each parent will take care of the cleanup of its own children. Only when you create a (pointer to) a widget/object which has no parent, you will need to explicitly delete it.

Upvotes: 1

drahnr
drahnr

Reputation: 6886

via addWidget you create a parent-child-relationship between these to widgets. Any parentless widget has to be destroyed manually, which in turn free its children recursivly. As long as your widget has a parent, and that is either freed manually or child to another Widget, no manual free is required.

Same accounts for QObject derived classes.

Upvotes: 0

Related Questions