PDA

View Full Version : Remove tabs from TabWidget



jmrbcu
7th April 2010, 22:27
Hi folks , I am using python, PyQt4 and I need to add and remove tabs from a QTabWidget after shown it. The widgets for tab pages are QVTKRenderWindowInteractor from VTK. What do I have to consider for doing this task, for example, flicker, objects deletion, etc.

PS: sorry for my english, it's not so good.

wysota
7th April 2010, 22:29
You call QTabWidget::addTab() and QTabWidget::removeTab() respectively.

jmrbcu
7th April 2010, 22:50
yes, I know that, how to delete the widget (QVTKRenderWindowInteractor) from memory, when I try:

interactor = tab_widget.widget(index)
tab_widget.removeTab(index)
interactor.close()

I think PyQt4 or Qt must call the overloaded deleteLater method in the QVTKRenderWindowInteractor (python code) but it does not get called.

jmrbcu
7th April 2010, 22:52
ohh, I forgot, __del__ method is jnot get called either

wysota
7th April 2010, 22:56
You have to check in PyQt docs how to enforce deletion of Qt objects. Can't help you much with that. It's possible that you have to call setParent(None) on each object you want deleted.

jmrbcu
7th April 2010, 23:07
setParent(None) doesn't work, the only thing that work is:

interactor = tab_widget.widget(index)
tab_widget.removeTab(index)
interactor.close()
interactor.deleteLater()

#optionally
del interactor

but if Qt documentation say that when you create a widget with the attribute: WA_DeleteOnClose, when the widget is closed, it will be deleted and I did this when I create the widget:

interactor = QVTKRenderWindowInteractor()
interactor.setAttribute(QtCore.Qt.WA_DeleteOnClose )

wysota
7th April 2010, 23:11
setParent(None) doesn't work, the only thing that work is:
Did you check in PyQt docs what works?

jmrbcu
7th April 2010, 23:31
the qt documentation for tabwidget is almost the same so, I don't know where to look.

wysota
8th April 2010, 00:12
I don't mean how to delete pages in tab widget but rather how to delete Qt objects in PyQt in general. I'm sure there is a dedicated call there somewhere to convince the Python garbage collector it should get rid of the object.

jmrbcu
8th April 2010, 00:24
I think that doing:

widget.close()
widget.deleteLater()
del widget

# and optionally this if U really must
import gc
gc.collect()

will do the trick