在 QLayout 中创建和删除自定义 QWidgets 时出现 RAM 问题
RAM problems while creating and deleting custom QWidgets inside of QLayout
我创建了一个自定义 QWidget
(下面的代码)[内部有一个 QHBoxLayout
和两个 QPushButtons
] 并将其添加到 GUI 中的 QVBoxLayout
。这个自定义 QWidget
-object 将被创建和删除几次(下面的代码)。
当我在控制台(在嵌入式 linux 上)输入 top
时,每次我添加新的 QWidget
时,RAM 都会增加。没关系!但是我看不到删除时 RAM 的减少。
我的代码有什么问题?我希望 RAM 在删除自定义 QWidgets
.
时减少
myCustomWidget.h
class QCustomPushButton_withinIcon_LeftAndRight : public QWidget {
Q_OBJECT
public:
//functions
QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent = 0);
~QCustomPushButton_withinIcon_LeftAndRight();
//other slots like:
// - virtual void mousePressEvent();
// - virtual void mouseReleaseEvent();
//other signals like:
// - void clicked();
//other functions like:
// - virtual void setEnabled(bool a);
// - virtual void paintEvent(QPaintEvent *);
// - ...
private:
//variables
QLayout *innerLayout; //!< Layout for two buttons
QPushButton *buttonLeft; //!< Button left
QPushButton *buttonRight; //!< Button right
//other variables like:
// - bool enabled;
// - ...
};
myCustomWidget.cpp
QCustomPushButton_withinIcon_LeftAndRight::QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent) :
QWidget(parent),
mSVG (new svg),
mGradient (new gradient)
{
enabled = true;
//create innerLayout
innerLayout = new QHBoxLayout();
this->setLayout(innerLayout);
//create buttons
buttonLeft = new QPushButton();
buttonRight = new QPushButton();
innerLayout->addWidget(buttonLeft);
innerLayout->addWidget(buttonRight);
//create connections like:
// - connect (buttonLeft, SIGNAL(pressed()), this, SLOT(mousePressEvent()));
// - ...
//set some stylesheets
// - buttonLeft->setStyleSheet("...");
}
QCustomPushButton_withinIcon_LeftAndRight::~QCustomPushButton_withinIcon_LeftAndRight()
{
//I think, that this is right. If not, correct me.
delete buttonLeft;
delete buttonRight;
delete innerLayout;
}
//void QCustomPushButton_withinIcon_LeftAndRight::mousePressEvent() {}
//void QCustomPushButton_withinIcon_LeftAndRight::mouseReleaseEvent() {}
//void QCustomPushButton_withinIcon_LeftAndRight::setEnabled(bool a) {}
//void QCustomPushButton_withinIcon_LeftAndRight::paintEvent(QPaintEvent *) {} ...
gui.cpp(将 QWidgets
添加到 GUI 中的 QLayout
)
QCustomPushButton_withinIcon_LeftAndRight *button = new QCustomPushButton_withinIcon_LeftAndRight();
//add button to layout
parentUiWindow->aLayoutNameInGui->addWidget(button);
//aLayoutNameInGui is type of QVBoxLayout
gui.cpp(在 GUI 中的 QLayout
中删除 QWidgets
)
//delete all buttons in layout
QLayoutItem *child;
while((child = parentUiWindow->aLayoutNameInGui->layout()->takeAt(0)) != 0) {
//parentUiWindow->aLayoutNameInGui->removeWidget(child->widget()); //already removed by ->takeAt()
//child->widget()->setParent(NULL);
delete child->widget();
delete child;
}
当您使用 top
命令查看内存使用情况时,您可能会得到误报。正如 一些程序员兄弟 所述,当您在某些对象上调用 delete
时,OS 并不总是从您的进程中释放分配的内存。
但是,您正在使用 new
:
在 QCustomPushButton_withinIcon_LeftAndRight
构造函数中创建两个对象
mSVG (new svg),
mGradient (new gradient)
但你似乎从来没有破坏过这些物品。所以你可能在那里有内存泄漏。
我创建了一个自定义 QWidget
(下面的代码)[内部有一个 QHBoxLayout
和两个 QPushButtons
] 并将其添加到 GUI 中的 QVBoxLayout
。这个自定义 QWidget
-object 将被创建和删除几次(下面的代码)。
当我在控制台(在嵌入式 linux 上)输入 top
时,每次我添加新的 QWidget
时,RAM 都会增加。没关系!但是我看不到删除时 RAM 的减少。
我的代码有什么问题?我希望 RAM 在删除自定义 QWidgets
.
myCustomWidget.h
class QCustomPushButton_withinIcon_LeftAndRight : public QWidget {
Q_OBJECT
public:
//functions
QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent = 0);
~QCustomPushButton_withinIcon_LeftAndRight();
//other slots like:
// - virtual void mousePressEvent();
// - virtual void mouseReleaseEvent();
//other signals like:
// - void clicked();
//other functions like:
// - virtual void setEnabled(bool a);
// - virtual void paintEvent(QPaintEvent *);
// - ...
private:
//variables
QLayout *innerLayout; //!< Layout for two buttons
QPushButton *buttonLeft; //!< Button left
QPushButton *buttonRight; //!< Button right
//other variables like:
// - bool enabled;
// - ...
};
myCustomWidget.cpp
QCustomPushButton_withinIcon_LeftAndRight::QCustomPushButton_withinIcon_LeftAndRight(QWidget *parent) :
QWidget(parent),
mSVG (new svg),
mGradient (new gradient)
{
enabled = true;
//create innerLayout
innerLayout = new QHBoxLayout();
this->setLayout(innerLayout);
//create buttons
buttonLeft = new QPushButton();
buttonRight = new QPushButton();
innerLayout->addWidget(buttonLeft);
innerLayout->addWidget(buttonRight);
//create connections like:
// - connect (buttonLeft, SIGNAL(pressed()), this, SLOT(mousePressEvent()));
// - ...
//set some stylesheets
// - buttonLeft->setStyleSheet("...");
}
QCustomPushButton_withinIcon_LeftAndRight::~QCustomPushButton_withinIcon_LeftAndRight()
{
//I think, that this is right. If not, correct me.
delete buttonLeft;
delete buttonRight;
delete innerLayout;
}
//void QCustomPushButton_withinIcon_LeftAndRight::mousePressEvent() {}
//void QCustomPushButton_withinIcon_LeftAndRight::mouseReleaseEvent() {}
//void QCustomPushButton_withinIcon_LeftAndRight::setEnabled(bool a) {}
//void QCustomPushButton_withinIcon_LeftAndRight::paintEvent(QPaintEvent *) {} ...
gui.cpp(将 QWidgets
添加到 GUI 中的 QLayout
)
QCustomPushButton_withinIcon_LeftAndRight *button = new QCustomPushButton_withinIcon_LeftAndRight();
//add button to layout
parentUiWindow->aLayoutNameInGui->addWidget(button);
//aLayoutNameInGui is type of QVBoxLayout
gui.cpp(在 GUI 中的 QLayout
中删除 QWidgets
)
//delete all buttons in layout
QLayoutItem *child;
while((child = parentUiWindow->aLayoutNameInGui->layout()->takeAt(0)) != 0) {
//parentUiWindow->aLayoutNameInGui->removeWidget(child->widget()); //already removed by ->takeAt()
//child->widget()->setParent(NULL);
delete child->widget();
delete child;
}
当您使用 top
命令查看内存使用情况时,您可能会得到误报。正如 一些程序员兄弟 所述,当您在某些对象上调用 delete
时,OS 并不总是从您的进程中释放分配的内存。
但是,您正在使用 new
:
QCustomPushButton_withinIcon_LeftAndRight
构造函数中创建两个对象
mSVG (new svg),
mGradient (new gradient)
但你似乎从来没有破坏过这些物品。所以你可能在那里有内存泄漏。