[PyQt] Fwd: Re: Python has stopped working in PyQt application

redstone-cold redstone-cold at 163.com
Fri Apr 3 06:12:24 BST 2015



Thanks very much !
so this 
self.widget = QtGui.QWidget(self)
will cause exist crash ,right ?






在2015年04月03 00时53分, "Baz Walter"<bazwal at ftml.net>写道:

On 02/04/15 06:06, redstone-cold wrote:
> For
> self.widget = QtGui.QWidget(self)
> Which is responsible for destroy self.widget when application exist ?
>

Both.

Like I said, there are two parts: a Python part and a Qt part.

Below is an example that should make it clear what is happening. If the
widget is given a parent, the C++ part will be deleted by Qt when the
main window closes. So if you try to call one of its Qt methods after
that, you will see an error like this:

    RuntimeError: wrapped C/C++ object of type QWidget has been deleted

But if the widget does not have a parent, the C++ part won't be
automatically deleted by Qt, and so there will be no error. In that
case, Python/PyQt will try to destroy the widget when the interpreter
exits (if sip.destroyonexit is True).


import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

mw = QMainWindow()
mw.setAttribute(Qt.WA_DeleteOnClose)

widget = QWidget(mw)
# widget = QWidget()
widget.setObjectName('widget')

mw.show()

app.exec_()

print(widget.objectName())

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150403/94277e3e/attachment.html>


More information about the PyQt mailing list