[PyQt] deleteLater vs setParent(None)

Dave Gradwell davegradwell at yahoo.co.uk
Tue May 10 17:42:37 BST 2016


Hi Elvis,

I’m doing something similar which works for me in production if that helps at all.
I preferred the explicitness of deleteLater, although I think either technique would work?

Best, Dave.


“”””””””””””””””””"
class Process(QtCore.QProcess):
    def __init__(self, singleshot=True, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.finished.connect(self.on_finished) # Important this is the first Slot for the Finished Signal (so that our outputText (etc) are readied).
        if singleshot:
            self.finished.connect(lambda: self.deleteLater())

        self.__outputText = ""
        self.__errorText = ""
        self.__succeeded = False

    def on_finished(self, exitCode, exitStatus):
        self.__outputText = str(self.readAllStandardOutput(), encoding='utf-8')
        self.__errorText = str(self.readAllStandardError(), encoding='utf-8')
        self.__succeeded = (exitCode == 0) & (exitStatus == self.NormalExit)

    def outputText(self):
        return self.__outputText

    def errorText(self):
        return self.__errorText

    def succeeded(self):
        return self.__succeeded

    def failed(self):
        # Slightly wary about this wording: for instance, returns true before even starting...
        return not self.__succeeded


class selfTerminatingProcess(Process):
    def __init__(self, timeToLive=1000, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.startTimer(timeToLive)

    def timerEvent(self, QTimerEvent):
        self.killTimer(QTimerEvent.timerId())
        self.close()
“”””””””””””””””””"




> On 10 May 2016, at 09:21, Elvis Stansvik <elvstone at gmail.com> wrote:
> 
> My question is about:
> 
>    process.finished.connect(process.deleteLater)
> 
> Can I count on that the Process object being destroyed eventually
> here, or should I rather use
> 
>    def cleanup():
>        process.setParent(None)
> 
>    process.finished.connect(cleanup)
> 
> That is, setting its parent to None.



More information about the PyQt mailing list