On Monday 04 November 2002 05:59 am, Kaleb Pederson wrote:
> Thanks.  Although it isn't usually necessary, it will be nice to have. 
> There is a bug in PyQt with respect to connections being removed when the
> associated object has been deleted. But I'll try to post some example code
> within the week.
>
> Basically, if I create a parentless object and connect signals to it.  Upon
> deletion of the object, any signals that would thereafter have gone to it
> result in a sip runtime error saying the C++ object has been deleted.

I just posted a message on the list and forgot to include an attachment.  Here 
it is.

--Kaleb

#!/usr/bin/python

from qt import *
import sys

class MyDialog(QDialog):
	
	def __init__(self,id = 0,parent = None,name = "MyDialog",modal=0,fl=0):
		QDialog.__init__(self,parent,name,modal,fl)

		self.id = id
		self.count = 0
		self.keep_ref = {}

		self.setCaption("Testing" + str(id))

		main_layout = QVBoxLayout(self,11,6,"main_layout")
		
		self.button1 = QPushButton(self,"button1")
		self.button1.setText(u'&Launch')
		
		main_layout.addWidget(self.button1)
		self.connect(self.button1,SIGNAL("clicked()"),self.launch)

		self.button2 = QPushButton(self,"button2")
		self.button2.setText(u'&Fire')

		main_layout.addWidget(self.button2)
		self.connect(self.button2,SIGNAL("clicked()"),self.fire)
		
		self.button3 = QPushButton(self,"button3")
		self.button3.setText(u'&Quit')

		main_layout.addWidget(self.button3)
		self.connect(self.button3,SIGNAL("clicked()"),self,SLOT("accept()"))
	
	def caught(self):
		print "dlg %i caught 'fire()' signal" % self.id
	
	def fire(self):
		self.emit(PYSIGNAL("fire()"), () )
	
	def closeEvent(self,event):
		print "dlg %i is closing" % self.id
		self.emit(PYSIGNAL("closing(id)"),(self.id,))
		event.accept()
	
	def accept(self):
		self.emit(PYSIGNAL("closing(id)"),(self.id,))
		QDialog.accept(self)
	
	def cleanup(self,id):
		print "cleaning up id %i" % id
		if id in self.keep_ref.keys():
			del self.keep_ref[id]
		print "references remaining:",self.keep_ref
	
	def launch(self):
		# keep a reference so it doesn't get destroyed
		dlg = MyDialog(self.count) # no parent
		self.connect(self,PYSIGNAL("fire()"),dlg.caught)
		
		# it works fine unless this signal is connected
		self.connect(self,PYSIGNAL("fire()"),dlg, SLOT("close()"))
		
		self.connect(dlg,PYSIGNAL("closing(id)"),self.cleanup)
		self.keep_ref[self.count] = dlg
		self.count += 1
		dlg.show()

qapp = QApplication(sys.argv)
mw = MyDialog()
qapp.setMainWidget(mw)
mw.show()
sys.exit(mw.exec_loop())

Reply via email to