PDA

View Full Version : Delete QWidget



StarShaper
29th February 2012, 14:00
Hi,

I'm a little bit confused about the safe deletion of a QWidget. If I create a QWidget with


MyWidget* myWidget = new MyWidget();

what is the correct way to delete the Widget?


delete myWidget;

or


myWidget.close();
delete myWidget;

or


myWidget.close();

wysota
29th February 2012, 14:32
myWidget->deleteLater();

close() doesn't delete anything, it just hides the widget.

code_err
29th February 2012, 14:47
function close() only return information whether your widget is closed (hidden) or not. It has nothing to do with memory release.

When you're creating QWidgets on a stack, for instance like that:


{
QWidget widget();
}

When it'll go out of scope {} widget will be delete automatically.

When you create QWidget on heap there are two ways

I. You give a pointer to parent as an argument when create widget and than you don't have to think about deleting it later:



QWidget *myWidget = new QWidget(parent)

usually parent = this, when you create widgets in constructor of your mainWindow widget

II. When you don't provide pointer to parent widget you have to delete it by yourself, you can do it either in destructor of one level up widget or
after closing your widget like that:



QWidget *myWidget = new QWidget();

myWidget->show();

if(myWidget->close)
{
delete myWidget;
}


But the second way looks strange and I don't know where it could be useful in typical applications.

I do it like that:
1. Derive my mainWindow from QWidget
2. In constructor create QWidgets on heap with parent pointer as arguments of QWidget constructors
3. Create myWindow widget in main.cpp on stack: MyWindow window; Then window.show();
4. Execute event loop and program after closing main window delete everything on its own.

wysota
29th February 2012, 14:57
@code_err: Are you sure that what you have written is correct?

For instance, what is this code going to do?


QWidget *myWidget = new QWidget();

myWidget->show();

if(myWidget->close)
{
delete myWidget;
}

code_err
29th February 2012, 15:55
Yeah, i know, this code does nothing and is not correct. Widget will never be deleted. If i want it to be deleted when widget gets hide i would probably do something with eventFilter but it is like shooting to fly with shotgun i think.

wysota
29th February 2012, 16:21
I would say this code wouldn't even compile since close() is a method.

If one wants to delete a widget when it's closed, one uses Qt::WA_DeleteOnClose attribute.

code_err
29th February 2012, 16:34
since close() is a method and it is returning void.Close is a method aka slot but it returns bool value not void.

Sorry, my fault. I do it all the time.
It should be of course close() but...

Closes this widget. Returns true if the widget was closed; otherwise returns false.I didn't see first line and thought that close() returns information whether QWidget was closed.

StarShaper
29th February 2012, 18:41
myWidget->deleteLater();



If I call deleteLater(), can I be sure that the Widget gets closed and that I can reuse the myWidget Pointer to create a new Widget immediately afterwards?



myWidget->deleteLater();
myWidget = new MyWidget();

What is the difference between


delete myWidget;

and


myWidget->deleteLater();

?

wysota
29th February 2012, 18:52
If I call deleteLater(), can I be sure that the Widget gets closed and that I can reuse the myWidget Pointer to create a new Widget immediately afterwards?
You can reuse the pointer any time you want, even without deleting the previous object assigned to it. When a widget is deleted, it gets removed from the screen if it's there.




myWidget->deleteLater();
myWidget = new MyWidget();

What is the difference between


delete myWidget;

and


myWidget->deleteLater();

?

The difference is described in the docs -- QObject::deleteLater()

StarShaper
29th February 2012, 20:33
You can reuse the pointer any time you want, even without deleting the previous object assigned to it.


And what happens to the object, if I reuse the pointer, without deleting the QWidget before? Is this some kind of auto_ptr, where the destructor is called automatically, when no pointer points to the object?

wysota
29th February 2012, 20:45
And what happens to the object, if I reuse the pointer, without deleting the QWidget before?
Nothing. It just sits somewhere in the memory. It's just the pointer is not tied to the object in any definite way.


Is this some kind of auto_ptr, where the destructor is called automatically, when no pointer points to the object?
No.

StarShaper
29th February 2012, 20:51
Nothing. It just sits somewhere in the memory. It's just the pointer is not tied to the object in any definite way.

Ok, that's actually my question. If I don't delete the object myself, than I have a memory leak, right?

wysota
29th February 2012, 22:17
If I don't delete the object myself, than I have a memory leak, right?
It depends. If the object has a parent or is owned by some other object, that object will take care of deleting it.

StarShaper
29th February 2012, 22:42
It depends. If the object has a parent or is owned by some other object, that object will take care of deleting it.

Ok, that was the point. Thanks! :)