I'm having issues with a non-Qt object falling out of scope and being garbage collected by Python. In the generic example below, I create an object when one button is clicked and want to use that object when the other button is clicked. After the slot for the 1st button is finished executing, the object is garbage collected.
# Started from http://zetcode.com/gui/pysidetutorial/firstprograms/ and
# connections program from chap 4 of Summerfield's PyQt book
import sys
from PySide import QtGui
def createThingy():
thing1 = thingy("thingName", 3, wid)
lbl1.setText(thing1.name + " created.")
return
def updateNum():
lbl1.setText("number: " + str(thing1.number))
return
class thingy(object):
def __init__(self, name, number, mainWindow):
self.name = name
self.number = number
def func1(self):
return "Something to return"
def rtnNum(self):
return "Num: " + str(self.number)
app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250, 150)
wid.setWindowTitle('Example')
btn1 = QtGui.QPushButton("Create thingy")
btn2 = QtGui.QPushButton("Number")
lbl1 = QtGui.QLabel("---")
vlayout = QtGui.QVBoxLayout()
vlayout.addWidget(btn1)
vlayout.addWidget(btn2)
vlayout.addWidget(lbl1)
wid.setLayout(vlayout)
btn1.clicked.connect(createThingy)
btn2.clicked.connect(updateNum)
wid.show()
wid.raise_()
sys.exit(app.exec_())
This sort of behavior is mentioned on the PySide pitfalls page, https://wiki.qt.io/PySide_Pitfalls, as well as the question "Python PySide (Internal c++ Object Already Deleted)" https://stackoverflow.com/a/5339238/2599816 (this question doesn't really answer my question since the author's solution is to avoid the problem).
Because the object is not a Qt object, passing it a parent is not a viable option. Additionally, I do not want to create the object at the start of the program as some other questions/answers have suggested elsewhere (can't find links to them again). Further, the object should not be modified to solve this problem but the solution should be implemented in the GUI, i.e. keep the object generic for use as a library in other programs.
- How do I make the object an attribute of my window or widget as suggested by the pitfalls page?
- While making it an attribute of the window/widget is an option, is there a better design/more elegant solution/best practice to use?
Thanks for the help.