[PyQt] Segfault when trying newDoc.setDocumentLayout(QtGui.QPlainTextDocumentLayout(newDoc))

Phil Thompson phil at riverbankcomputing.com
Mon Feb 2 09:33:53 GMT 2009


On Mon, 02 Feb 2009 03:37:13 +0100, GT6 <gt6 at gmx.net> wrote:
> Hi there.
> 
> When running the below sample script, and clicking the button, it will
> segfault, and I don't understand why or how. I've overwritten a function
> in QPlainTextEdit and it should create a new document, set the
> PlainTextDocumentLayout and then set the document on the QPlainTextEdit
> widget. The important part is this (1st way):
> 
>         newDoc = QtGui.QTextDocument()
>         newDoc.setDocumentLayout(QtGui.QPlainTextDocumentLayout(newDoc))
>         self.setDocument(newDoc)
> 
> The above segfaults. I also tried this (2nd way):
> 
>             newDoc = QtGui.QTextDocument()
>             newDoc.setDocumentLayout(self.documentLayout())
>             self.setDocument(newDoc)
> 
> This one doesn't segfault, but I get this error:
> 
> AttributeError: documentLayout
> 
> Apart from that, I tried this third option (3rd way):
> 
>         newDoc = QtGui.QTextDocument()
> 
> newDoc.setDocumentLayout(QtGui.QPlainTextDocumentLayout.__init__(newDoc))
>         self.setDocument(newDoc)
> 
> with the only difference from the first one being that __init__ is
> called explicitly instead of expecting it to be defaultet. This one
> _doesn't_ segfault, but gives this error:
> 
> QPlainTextEdit::setDocument: Document set does not support
> QPlainTextDocumentLayout
> 
> Which makes no sense as not calling __init__ directly shouldn't be any
> different than calling it explicitly.

__init__() initialises a class, it doesn't create one.

> So, does anyone have any idea why
> this is happening? Do you have a bug tracker?

Keep a reference to "newDoc" - it is getting garbage collected too soon.

Phil


More information about the PyQt mailing list