0

我有一个关于 Qt 的问题。我想知道我应该如何删除我创建的所有指针。例如:

.h 文件

#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 文件:

#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();
    }
}

我被困在这里。我是否应该删除所有指针(QPushbutton 等)和子类,以免发生内存泄漏?

4

2 回答 2

1

简短回答:不,您不必明确删除它们。

在 Qt 中,QObject 被组织在对象树中。各种小部件的父子关系也意味着父拥有子小部件的所有权。

因此,您不必在应用程序完成时显式删除它们。每个父母都会负责清理自己的孩子。只有当您创建(指向)没有父级的小部件/对象时,您才需要显式删除它。

于 2012-08-19T20:04:55.623 回答
0

通过addWidget您在这些与小部件之间创建父子关系。任何无父小部件都必须手动销毁,这反过来又递归地释放其子级。只要您的小部件具有父级,并且该父级是手动释放的,或者是另一个小部件的子级,则不需要手动释放。

QObject 派生类的帐户相同。

于 2012-08-19T20:07:31.700 回答