所以我有類調用AppointmentSchedule
這是以下類型:QList清除函數調用是否清除存儲在QList中的動態分配對象的內存?
namespace Ui {
class AppointmentSchedule;
}
class AppointmentSchedule : public QWidget
{
Q_OBJECT
public:
explicit AppointmentSchedule(QWidget *parent = 0);
~AppointmentSchedule();
Ui::AppointmentSchedule *ui;
};
這個類的ui
由QLabel
類型的兩個對象和QDateTimeEdit
類型的兩個對象。在外部類中,我需要有一個類型的動態分配對象的QList
。我將在QList
中追加動態分配的對象,類似於以下方式。
QList<AppointmentSchedule*> scheduleList;
foreach (QDate date, dates)
{
AppointmentSchedule * newSchedule = new AppointmentSchedule(this);//Allocation of memory
QDateTime sDateTime(date, QTime(8, 0, 0));
newSchedule->ui->appointmentStartDateTimeEdit->setDateTime(sDateTime);
QDateTime eDateTime(date, QTime(8, 15, 0));
newSchedule->ui->appointmentEndDateTimeEdit->setDateTime(eDateTime);
scheduleList.append(newSchedule);
}
如果我叫QList
這樣的clear()
功能:
scheduleList.clear();
將它釋放了所有我所分配的內存?
謝謝。
謝謝,我很快就會接受你的回答。我實際上已經按照你所說的關於在你寫答案之前手動刪除它們。所以,我們同意這一點。 :) –
但是在提供的代碼中沒有內存泄漏。 AppointmentSchedule對象具有父對象,這就是爲什麼當父對象被銷燬時這些對象將被刪除。 –
@the_naive請注意基里爾的答案。你爲你的對象設置非nullptr父母,所以父母的析構函數將刪除它們,你不需要手動刪除它們。這是Qt對象層次結構的一個特性,也是刪除嵌套窗口小部件的常用方法。 – Sergey