在 QLayout 中创建和删除自定义 QWidgets 时出现 RAM 问题

RAM problems while creating and deleting custom QWidgets inside of QLayout

本文关键字:RAM 问题 QWidgets 自定义 QLayout 创建 删除      更新时间:2023-10-16

我创建了一个自定义QWidget(下面的代码([里面有一个QHBoxLayout和两个QPushButtons]并将其添加到GUI中的QVBoxLayout中。此自定义QWidget-对象将被多次创建和删除(下面的代码(。

当我在控制台中键入top时(在嵌入式 Linux 上(,每次添加新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;
// - ...
};

我的自定义小部件.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时,操作系统并不总是从进程中释放分配的内存。

但是,您将使用newQCustomPushButton_withinIcon_LeftAndRight构造函数中创建两个对象:

mSVG (new svg),
mGradient (new gradient)

但你似乎永远不会摧毁这些物体。所以你可能在那里有内存泄漏。