[PyQt] Segfault with QString

Phil Thompson phil at riverbankcomputing.com
Sat Mar 15 16:36:15 GMT 2008


On Saturday 15 March 2008, Filip Gruszczyński wrote:
> First I would like to say hello to everyone here, since it's my first mail.
>
> I have certain problem with QString, which tend to crash my program,
> when casted to a string. I have following code:
>
> class Element(QObject):
>
> 	def __init__(self, name, content = None, value = None):
> 		QObject.__init__(self)
> 		self.__name = name
> 		self.__content = content
> 		self.__value = value
>
> 	def getValue(self):
> 		return self.__value
>
> 	def setValue(self, value):
> 		self.__value = value
>
> 	def getName(self):
> 		return self.__name
>
> 	def hasContent(self):
> 		return self.__content != None
>
> 	def getContent(self):
> 		return self.__content
>
> 	def setContent(self, content):
> 		self.__content = content
>
> class Attribute(Element):
>
> 	def __init__(self, name, content):
> 		Element.__init__(self, name, content)
>
> 	def isFinal(self):
> 		return True
>
> 	def getValues(self):
> 		return self.getContent().getValues()
>
> 	def __repr__(self):
> 		return str(self.getName()) + " " + str(self.getValue()) + "\n"
>
> And in some other place:
>
> self.connect(box, SIGNAL('currentIndexChanged(QString)'),
> attribute.setValue);
>
> This works perfect, untial I call print on Attribute object, then the
> program crashes.
>
> However, If I change code to:
>
> def setValue(self, value):
> 	self.__value = str(value)
>
> which is basically moving cast to some earlier point, everything works
> fine. Anyone has any idea, why this is wrong? Though it's already
> working, I would like to know, why it didn't with previous version.

It's practically impossible to help if you just post a code fragment. The 
problem is almost certain to be in the bits of code you haven't posted.

Try and come up with a simple but complete script that demonstrates the 
problem so that other people can reproduce it.

Phil



More information about the PyQt mailing list