PDA

View Full Version : deleteLater of Custom Class



santosh.kumar
5th November 2016, 09:20
Hi

I am using Qt 4.8.7 on Windows.

Sample of some code


class MyWidgt: public QWidget
{
Q_OBJECT
public:
MyWidgt(QWidget *parent = 0);
~MyWidget();
private:
QPushButton *p1;
QLabel *p2;
};
MyWidgt::MyWidgt(QWidget)
{
p1 = new QPushButton;
p2 = new QPushButton;
}
MyWidgt::~MyWidgt()
{
p1->deleteLater();
p2->deleteLater();
}
int main(int argc, char*argv[])
{
MyWidgt *qq = new MyWidgt;
qq->deleteLater();

or delete qq;
}

Kindly tell me which is the best process...
qq->deleteLater();
or delete qq;

anda_skoa
5th November 2016, 13:02
Usually p1 and p2 would be children of MyWidget and thus be deleted by MyWidget anyway.

But if you have objects that need deleting like that, just "delete"

Cheers,
_

santosh.kumar
5th November 2016, 16:19
Hi
If I do not delete p1 and p2 in destructor, simply qq->deleteLater(), can p1 and p2 as a child will automatically deleted, kindly confirm it. I have a lot of widget allocated in constructor, if I do not delete it individually and simply delete class's object using delete later then it would delete all or not. Kindly confirm it. I am confused..

Thanks
Santosh

anda_skoa
6th November 2016, 12:57
It depends if those other widgets, in your case those buttons, are children of another widget.
Usually that will be the case, i.e. the buttons are part of a widget, not stand-alone windows.

In your code snippet above they are not, but then again that is almost certainly not your real code.

Cheers,
_