代码之家  ›  专栏  ›  技术社区  ›  Michael1248

在QLayout内部创建和删除自定义QWidget时出现RAM问题

  •  0
  • Michael1248  · 技术社区  · 7 年前

    我创建了一个自定义 QWidget QHBoxLayout 还有两个 QPushButtons [inside]并将其添加到 QVBoxLayout QWidget

    当我打字时 top QWidget

    我的代码有什么问题?我想,删除自定义后RAM会减少 QWidgets .

    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;
        // - ...
    };
    

    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 *) {} ...
    

    图形用户界面。cpp(添加 QWidgets 到a QLayout

        QCustomPushButton_withinIcon_LeftAndRight *button = new QCustomPushButton_withinIcon_LeftAndRight();
        //add button to layout
        parentUiWindow->aLayoutNameInGui->addWidget(button);
        //aLayoutNameInGui is type of QVBoxLayout
    

    图形用户界面。cpp(删除 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;
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   thuga    7 年前

    当您使用 top 命令,您可以得到误报。如所述 一些程序员 delete 在一些物体上。

    QCustomPushButton_withinIcon_LeftAndRight 具有的构造函数 new

    mSVG (new svg),
    mGradient (new gradient)