PDA

View Full Version : Qt widget setParent



bunjee
30th November 2007, 17:33
Hey there,

My app is heavily based on widget.
For some reason they don't seem to be deleted.
I've come up with the following settable widget code :


#include "ZeSettableLayout.h"

#include "ZeLog.h"

//================================================== ===========================
//================================================== ===========================

ZeSettableLayout::ZeSettableLayout(QWidget * parent) :
QWidget(parent),
mLayout(this)
{
mSettableLayout = 0;
mWidget = 0;

getLayout().setMargin(0);
getLayout().setSpacing(0);

mOldParent = NULL;
}

//================================================== ===========================
//================================================== ===========================

ZeSettableLayout::~ZeSettableLayout()
{
Clear();
}

//================================================== ===========================
//================================================== ===========================

void ZeSettableLayout::setLayout(QLayout & settableLayout,
int stretch)
{
Clear();

mSettableLayout = &settableLayout;

getLayout().addLayout(mSettableLayout, stretch);
}

//================================================== ===========================
//================================================== ===========================

void ZeSettableLayout::setWidget(QWidget & widget,
int stretch,
Qt::Alignment alignment)
{
Clear();

mWidget = &widget;

// Saving old parent to restore it at deletion
mOldParent = getWidget().parentWidget();

getLayout().addWidget(mWidget, stretch, alignment);
getWidget().show();
}

//================================================== ===========================
//================================================== ===========================

void ZeSettableLayout::Clear()
{
if (mSettableLayout)
{
getLayout().removeItem(mSettableLayout);

mSettableLayout = NULL;
}

if (mWidget)
{
getLayout().removeWidget(mWidget);

// Retoring old parent for deletion
getWidget().setParent(mOldParent);
mOldParent = NULL;

//getWidget().hide();
mWidget = NULL;
}
}

At some point I'm backuping the old widget's parent, to set it back when my settable widget is deleted :
line 77 getWidget().setParent(mOldParent);

It seems to prevent the widget from deletion, even when I'm calling delete widget, I'm not sure why.

jpn
30th November 2007, 17:45
You're asking for troubles by allocating widgets and layouts on the stack... It's not really safe, once a parent deletes a child allocated on the stack you'll get a crash.

bunjee
30th November 2007, 18:12
You're not the first to tell me so.

Actually,
I'm not sure what is wrong with allocating a Widget / Layout on the stack.

Of course if you use them badly it will most likely crash,
but,
why declaring a layout on an instance's stack would crash anything ?

You're telling me that a widget on the stack won't be properly deleted in the dtor of my classe instances ?

Because for me in C++ and it's common knowledge declaring something on the stack has never crashed anything.

jpn
30th November 2007, 18:49
You're not the first to tell me so.
Doesn't that ring any bells? :)

Actually,
I'm not sure what is wrong with allocating a Widget / Layout on the stack. Of course if you use them badly it will most likely crash,
but,
why declaring a layout on an instance's stack would crash anything ?
It's not about using objects allocated on the stack "badly" or "properly". It's because parents take ownership of their children. A parent deletes all of its children. A parent widget deletes its layout. A parent widget deletes its child widgets. When a parent deletes something that was allocated on stack, you'll get a crash.

You're telling me that a widget on the stack won't be properly deleted in the dtor of my classe instances ?
A constructor gets called whenever an object gets destructed. For stack objects this happens when they go out of scope.

Gopala Krishna
30th November 2007, 18:49
QObjects store children as a list containing QObject pointers. When you delete the object or QObject destructor is called, it calls delete on all its children.

Now you can see the scenario in your case where you allocate QObjects on a stack and it does have parent. When parent is destructed , delete is called on object on stack which crashes your program.

edit: I was bit late(jpn answered first) but atleast better late than never ;)

bunjee
30th November 2007, 19:00
I don't have any crash with my application :).

My problem is memory.

According to Qt Doc :

Warning: It is very unlikely that you will ever need this function. If you have a widget that changes its content dynamically, it is far easier to use QStackedWidget.

Does setting parent to compromise anything for deletion I'm not aware off ?

jpn
30th November 2007, 19:39
It seems to prevent the widget from deletion, even when I'm calling delete widget, I'm not sure why.
What does this mean, actually? What do you mean with "prevent something from deletion"?


You're not the first to tell me so.
Indeed, seems you've been warned before in this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-qt-layout-memory-issue-8715.html) by a bunch of people. Why do you insist doing it wrong? Have you seen any Qt examples allocating widgets or layouts on the stack (excluding main() and modal dialogs)? Or did you ever actually read any of them? Sorry for the tone, but I just don't understand people who regardless of continuous warnings insist of doing things their own (wrong) way.


My problem is memory.
What exact steps did you take to verify this?

wysota
30th November 2007, 20:07
You're not the first to tell me so.

Actually,
I'm not sure what is wrong with allocating a Widget / Layout on the stack.

Of course if you use them badly it will most likely crash,
but,
why declaring a layout on an instance's stack would crash anything ?

You're telling me that a widget on the stack won't be properly deleted in the dtor of my classe instances ?

You are considering yourself an expert, so I'll give you an "expert" reply - allocating layouts and child widgets on the stack causes crashes, because of trying to deallocate the same area of memory twice. As you are an expert, you'll surely know what I'm talking about.



Because for me in C++ and it's common knowledge declaring something on the stack has never crashed anything.

Oh really? Consider this:


int main(){
int x;
delete &x;
return 0;
}
Is this going to crash the application or not?

bunjee
30th November 2007, 20:26
So you mean there is a "delete" layout and a "delete" widget in the Qt code.

In that case I don't understand why my program is not crashing.

I'll go check the sources to see how this works.

wysota
30th November 2007, 20:38
Because you are lucky and the order in which the compiler deletes objects that goes out of scope is exactly the one that is needed to avoid a crash. I don't want to inspect your code right now, so I'll give an easy example:

#include <QObject>
int main(int argc, char **argv){
QObject o1;
QObject o2;
o2.setParent(&o1);
return 0;
}

Run this application and see if it crashes. Then switch lines 3. and 4. and run it again and again see if it crashes.