【发布时间】:2017-08-08 13:40:31
【问题描述】:
我创建了一个自定义QWidget(代码如下)[内部有一个QHBoxLayout 和两个QPushButtons],并将它添加到GUI 中的QVBoxLayout。这个自定义的QWidget-object 将被创建和删除多次(代码如下)。
当我在控制台(在嵌入式 linux 上)中键入 top 时,每次添加新的 QWidget 都会增加 RAM。没关系!但我看不到删除时 RAM 的减少。
我的代码有什么问题?我希望,删除自定义 QWidgets 时 RAM 会减少。
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(在 GUI 中将 QWidgets 添加到 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;
}
【问题讨论】:
-
我确信关于这个问题肯定有一些重复。但是“问题”很可能是当您
delete或以其他方式释放内存时,操作系统不必从您的进程中取消映射分配的内存页面。相反,它可以在任何感觉的时候做到这一点。这意味着这些页面看起来像是内存泄漏。这是误报。 -
如果您将函数声明放在代码中而不是“gui.cpp(将 QWidgets 添加到 GUI 中的 QLayout)”中,将更容易阅读和复制您的代码
-
我看到你用
new创建了mSVG和mGradient,但是你会在任何地方销毁这些对象吗? -
@Someprogrammerdude 你真的舒尔吗?如果这是真的,那就太好了。
-
@thuga 我试过了,将它们删除到.. 之后应用实际上做得更好