PyQt4 segmentation fault when assigning text to QGraphicsSimpleTextItem

943 views Asked by At

I'm using Python 2.7.5 on Ubuntu 13.10 and the PyQt4 version is 4.10.2. I'm new to PyQt and the following code demonstrates how I get the segmentation fault. after starting the python interpreter:

Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt4 import QtGui
>>> obj=QtGui.QGraphicsSimpleTextItem()
>>> obj.setText('sometext')
Segmentation fault (core dumped)

or initialize directly with string:

>>> from PyQt4 import QtGui
>>> obj2=QtGui.QGraphicsSimpleTextItem('some text')
Segmentation fault (core dumped)

or set text with QString object:

>>> from PyQt4 import QtGui, QtCore
>>> s=QtCore.QString('sometext')
>>> obj=QtGui.QGraphicsSimpleTextItem()
>>> obj.setText(s)
Segmentation fault (core dumped)

I must have missed something very basic. Please help. Thanks.

1

There are 1 answers

2
ekhumoro On BEST ANSWER

You should always create an QApplication instance before trying to use GUI objects/widgets:

Python 2.7.6 (default, Nov 26 2013, 12:52:49) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt4 import QtGui
>>> app = QtGui.QApplication([])
>>> obj = QtGui.QGraphicsSimpleTextItem()
>>> obj.setText('foo')
>>> 

Its a good idea to add a function to your .pythonrc.py which does all the necessary imports and setup for a pyqt interactive session - saves a lot of tedious re-typing!