2

What I want is to completely remove a widget (purge it, delete it, etc...), but it's in a gridlayout, so even when calling removeWidget, it still keeps a pointer, so python doesn't want to remove the object. Here is the (stripped) code:

def addRow(self, row):
    self.entries.insert(row, QtGui.QLineEdit())
    self.gridlayout.addWidget(self.entries[row], row, 0)
...
def remRow(self, row):
    self.gridlayout.removeWidget(self.entries[row])
    del(self.entries[row])
...
(in another function)
foo.addRow(0)
foo.remRow(0)

It removes the widget from the gridlayout, but it doesn't remove it completely, so it actually gets packed beneath(?) the layout, and the widget is apparently bigger than the layout (not sure though, as I cannot see the end).

So again, is there any way to completely remove a widget that was inside a QGridLayout?

Thanks in advance!

MiJyn
  • 5,327
  • 4
  • 37
  • 64

2 Answers2

5

Layouts re-parent the widgets to the containers. So even if you remove it from the layout, container widget is still the parent, so that widget is not removed. You should call .deleteLater() to tell Qt to get rid of that widget (effectively clearing all references for the widget so that Python can purge it):

def remRow(self, row):
    self.gridlayout.removeWidget(self.entries[row])
    self.entries[row].deleteLater()
    del self.entries[row]
Avaris
  • 35,883
  • 7
  • 81
  • 72
  • Explicit removing from the layout is not really necessary, but if you do it, you should also hide the widget, to make sure you do not get one funny paintEvent before deleteLater actually does it's thing. – hyde Nov 01 '12 at 21:25
1

If you want to just get rid of the widget for good, just delete it. From Python, you may have to call widget's deleteLater() method. Qt should take care of the rest, like removing it from layout etc.

I'm not sure what the del in your question does, but apparently it does not do C++ delete.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • "If you want to just get rid of the widget for good, just delete it", yep, that was exactly my question ^_^ – MiJyn Nov 01 '12 at 20:04