Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this PyQt 4 python bug or wrongly behaving code?

Following code should create the QGraphicsView widget which owns one QGraphicsScene having text inside of it:

#!/usr/bin/python

import sys
from PyQt4.QtGui import *


if __name__ == '__main__':
  app = QApplication(sys.argv)

  view = QGraphicsView()  
  scene = QGraphicsScene()

  scene.addText("Hello!")

  view.setScene(scene)
  view.show();

  sys.exit(app.exec_())

This opens the window, puts the text there, but after I close window - python dumps core and several issues are printed out:

(python:5387): Gtk-CRITICAL **: IA__gtk_container_add: assertion `GTK_IS_CONTAINER (container)' failed

(python:5387): Gtk-CRITICAL **: IA__gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed
...clip...
... above message is shown many, many times ...
...clip...
(python:5387): Gtk-CRITICAL **: IA__gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed
Segmentation fault (core dumped)

Versions: python2.7 2.7.3-0ubuntu3.1 python-qt4 4.9.1-2ubuntu1

like image 856
jhnsn Avatar asked Sep 15 '12 00:09

jhnsn


1 Answers

It looks to be related to the QApplication object being deleted when quitting, but I'm not sure why. Your code worked fine for me under Windows but I got the same seg fault output as you under an Ubuntu install.

I managed to get a clean exit with the following code as a work around.

#!/usr/bin/python

import sys
from PyQt4.QtGui import QApplication, QGraphicsView, QGraphicsScene


if __name__ == '__main__':
  app = QApplication(sys.argv)

  view = QGraphicsView()  
  scene = QGraphicsScene()

  scene.addText("Hello!")

  view.setScene(scene)
  view.show()

  app.exec_()
  app.deleteLater()
  sys.exit()
like image 163
Gary Hughes Avatar answered Oct 13 '22 11:10

Gary Hughes