Memory leak from promoted class of QDoubleSpinBox
-
I am promoting the QDoubleSpinBox class as i want to catch the mouseDoubleClick Event.
This is the Promoted class.
class SumDoubleBox : public QDoubleSpinBox { Q_OBJECT public: explicit SumDoubleBox(QWidget* parent = nullptr); void stepBy(int steps) override; protected: virtual void focusInEvent(QFocusEvent *e) override; public slots: void setZero(); signals: int signalUndoRedo(); private: double m_defaultStep = 1.0; }; SumDoubleBox::SumDoubleBox(QWidget* parent) : QDoubleSpinBox(parent) { SumLineEdit* lineEdit = new SumLineEdit(this); setLineEdit(lineEdit); setMinimum(0.0); setMaximum(99999.0); }Since i am creating a pointer in the Constructor of the SumDoubleBox Class.
SumLineEdit* lineEdit = new SumLineEdit(this); ``Do i need to Explicitly delete this in the Destructor ?
Defination of SumLineEdit Class.class SumLineEdit : public QLineEdit { Q_OBJECT public: explicit SumLineEdit(QWidget* parent = nullptr) { }; protected: void mouseDoubleClickEvent(QMouseEvent* event) override; }; void SumLineEdit::mouseDoubleClickEvent(QMouseEvent* event) { if (event->button() == Qt::LeftButton) { selectAll(); event->accept(); return; } QLineEdit::mouseDoubleClickEvent(event); } -
I am promoting the QDoubleSpinBox class as i want to catch the mouseDoubleClick Event.
This is the Promoted class.
class SumDoubleBox : public QDoubleSpinBox { Q_OBJECT public: explicit SumDoubleBox(QWidget* parent = nullptr); void stepBy(int steps) override; protected: virtual void focusInEvent(QFocusEvent *e) override; public slots: void setZero(); signals: int signalUndoRedo(); private: double m_defaultStep = 1.0; }; SumDoubleBox::SumDoubleBox(QWidget* parent) : QDoubleSpinBox(parent) { SumLineEdit* lineEdit = new SumLineEdit(this); setLineEdit(lineEdit); setMinimum(0.0); setMaximum(99999.0); }Since i am creating a pointer in the Constructor of the SumDoubleBox Class.
SumLineEdit* lineEdit = new SumLineEdit(this); ``Do i need to Explicitly delete this in the Destructor ?
Defination of SumLineEdit Class.class SumLineEdit : public QLineEdit { Q_OBJECT public: explicit SumLineEdit(QWidget* parent = nullptr) { }; protected: void mouseDoubleClickEvent(QMouseEvent* event) override; }; void SumLineEdit::mouseDoubleClickEvent(QMouseEvent* event) { if (event->button() == Qt::LeftButton) { selectAll(); event->accept(); return; } QLineEdit::mouseDoubleClickEvent(event); }@summit said in Memory leak from promoted class of QDoubleSpinBox:
Do i need to Explicitly delete this in the Destructor ?
No, because
new SumLineEdit(this)https://doc.qt.io/qt-6/qabstractspinbox.html#setLineEditQAbstractSpinBox takes ownership of the new lineEdit
-
@summit said in Memory leak from promoted class of QDoubleSpinBox:
Do i need to Explicitly delete this in the Destructor ?
No, because
new SumLineEdit(this)https://doc.qt.io/qt-6/qabstractspinbox.html#setLineEditQAbstractSpinBox takes ownership of the new lineEdit