[PyQt] Deleting object in event handler

David Boddie david at boddie.org.uk
Tue Nov 25 00:31:49 GMT 2008


On Fri, 21 Nov 2008 20:17:36 +0100, Filip Gruszczy?ski wrote:

> 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?

I don't see those (Qt 4.3.2, PyQt 4.3) but I imagine it can happen under
certain circumstances.

> 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?

What you have should basically work. When a check box is hidden the layout
should take care of the arrangement of the ones that remain.

> 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()

Do you need to set the parent? What happens if you just hide the check box?

David



More information about the PyQt mailing list