[PyQt] Deleting object in event handler

Filip Gruszczyński gruszczy at gmail.com
Fri Nov 21 19:17:36 GMT 2008


I want to implement mechanism similar to the one used in GMail
attachments, where by unchecking check box you remove an attachment
from the mail. I have created following piece of code, which should
remove options when they are unchecked, but unfortunately I receive
warnings when I set parent to None. Can you make it right somehow? Do
I have to keep some separate set of unchecked boxes and delete them
from time to time? That wouldn't be very elegant - I prefer to remove
them at the moment, when user wants them removed. Could anyone suggest
me something?

	class QChoices(QWidget):
	
		def __init__(self):
			QWidget.__init__(self)
			self.setLayout(QVBoxLayout())
			
		def addChoice(self, choice):
			checkBox = QCheckBox(choice)
			checkBox.setCheckState(Qt.Checked)
			self.connect(checkBox, SIGNAL('stateChanged(int)'), self.remove)
			self.layout().addWidget(checkBox)
			
		def remove(self, state):
			if state != Qt.Unchecked:
				raise Exception('Choice checked back again!')
			self.sender().hide()
			self.sender().setParent(None)
			self.updateGeometry()

-- 
Filip Gruszczyński



More information about the PyQt mailing list