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
) :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;
}
}
#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;
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks