在 QLayout 中创建和删除自定义 QWidgets 时出现 RAM 问题
RAM problems while creating and deleting custom QWidgets inside of QLayout
我创建了一个自定义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
时,操作系统并不总是从进程中释放分配的内存。
但是,您将使用new
在QCustomPushButton_withinIcon_LeftAndRight
构造函数中创建两个对象:
mSVG (new svg),
mGradient (new gradient)
但你似乎永远不会摧毁这些物体。所以你可能在那里有内存泄漏。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 在 QLayout 中创建和删除自定义 QWidgets 时出现 RAM 问题
- 为什么我的C 代码在Linux上运行,而在Windows上不运行,是否有RAM内存的问题