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

Baz Walter bazwal at ftml.net
Thu Apr 2 17:53:14 BST 2015


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



More information about the PyQt mailing list