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 :

Qt Code:
  1. #include "ZeSettableLayout.h"
  2.  
  3. #include "ZeLog.h"
  4.  
  5. //=============================================================================
  6. //=============================================================================
  7.  
  8. ZeSettableLayout::ZeSettableLayout(QWidget * parent) :
  9. QWidget(parent),
  10. mLayout(this)
  11. {
  12. mSettableLayout = 0;
  13. mWidget = 0;
  14.  
  15. getLayout().setMargin(0);
  16. getLayout().setSpacing(0);
  17.  
  18. mOldParent = NULL;
  19. }
  20.  
  21. //=============================================================================
  22. //=============================================================================
  23.  
  24. ZeSettableLayout::~ZeSettableLayout()
  25. {
  26. Clear();
  27. }
  28.  
  29. //=============================================================================
  30. //=============================================================================
  31.  
  32. void ZeSettableLayout::setLayout(QLayout & settableLayout,
  33. int stretch)
  34. {
  35. Clear();
  36.  
  37. mSettableLayout = &settableLayout;
  38.  
  39. getLayout().addLayout(mSettableLayout, stretch);
  40. }
  41.  
  42. //=============================================================================
  43. //=============================================================================
  44.  
  45. void ZeSettableLayout::setWidget(QWidget & widget,
  46. int stretch,
  47. Qt::Alignment alignment)
  48. {
  49. Clear();
  50.  
  51. mWidget = &widget;
  52.  
  53. // Saving old parent to restore it at deletion
  54. mOldParent = getWidget().parentWidget();
  55.  
  56. getLayout().addWidget(mWidget, stretch, alignment);
  57. getWidget().show();
  58. }
  59.  
  60. //=============================================================================
  61. //=============================================================================
  62.  
  63. void ZeSettableLayout::Clear()
  64. {
  65. if (mSettableLayout)
  66. {
  67. getLayout().removeItem(mSettableLayout);
  68.  
  69. mSettableLayout = NULL;
  70. }
  71.  
  72. if (mWidget)
  73. {
  74. getLayout().removeWidget(mWidget);
  75.  
  76. // Retoring old parent for deletion
  77. getWidget().setParent(mOldParent);
  78. mOldParent = NULL;
  79.  
  80. //getWidget().hide();
  81. mWidget = NULL;
  82. }
  83. }
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.