gpt4 book ai didi

c++ - 删除创建的 Qt 元素和子类

转载 作者:行者123 更新时间:2023-11-28 07:58:52 27 4
gpt4 key购买 nike

我有一个关于 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 等)和子类,以免发生内存泄漏?

最佳答案

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

在 Qt 中,QObjects 是 organized in object trees .各种小部件的父子关系也意味着父级拥有子小部件的所有权。

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

关于c++ - 删除创建的 Qt 元素和子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12029558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com