我知道有很多关于qt内存管理的问题,但是我找不到我的答案。Qt对象的Qt内存管理
因此,对于qt小部件,如果您在创建对象时将此参数(这是父级小部件)传递给父级小部件,则父级小部件将取得它的所有权并在父级死亡时将其删除。 将小部件添加到布局并在小部件上设置该布局时会发生同样的情况。
但是,如果我声明一个指向QColor对象的指针会发生什么,例如? QColor不能作为其他小部件的子项添加或不能添加到布局中。当部件死亡或者我需要在析构函数中手动删除它时,内存是否会被释放?
这里有一个例子...当Widget被销毁时m_pColor会发生什么?它会被销毁还是会有内存泄漏? 对不起,代码中的任何错误,我没有编译它,我只是写在这里作为例子。
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QHBoxLayout;
class QPushButton;
class QColor;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = NULL);
public:
QHBoxLayout *m_pLayout;
QPushButton *m_pButton;
QColor *m_pColor;
};
#endif // WIDGET_H
#include "widget.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QColor>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
m_pLayout = new QHBoxLayout;
m_pButton = new QPushButton(tr("Button"));
m_pLayout->addWidget(m_pButton);
m_pColor = new QColor(0, 0, 0, 255);
setLayout(m_pLayout);
}