这是 PyQt 4 python 错误还是行为错误的代码? [英] Is this PyQt 4 python bug or wrongly behaving code?

查看:55
本文介绍了这是 PyQt 4 python 错误还是行为错误的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码应创建 QGraphicsView 小部件,该小部件拥有一个其中包含文本的 QGraphicsScene:

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_())

这会打开窗口,将文本放在那里,但是在我关闭窗口后 - python 转储核心并打印出几个问题:

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)

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

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

推荐答案

这可能既不是 PyQt 错误,也不是错误行为的代码.

This is probably neither a PyQt bug, nor wrongly behaving code.

当 python 完成关闭过程时,对象被删除的顺序是不可预测的.有时,这可能会导致出现一些令人困惑的错误消息.

When python goes through its shutdown process, the order in which objects get deleted can be unpredictable. Occasionally, this may cause somewhat baffling error messages to appear.

您的脚本在我的(非 Ubuntu)Linux 机器上运行良好 - 但是当我关闭窗口时,我得到以下输出:

Your script runs fine on my (non-Ubuntu) Linux machine - but when I close the window, I get this output:

$ python2 test.py 
QPixmap: Must construct a QApplication before a QPaintDevice
Aborted

从表面上看,这似乎完全没有意义......

Which, taken at face value, seems to make no sense at all...

但是,通过强制以不同的顺序删除对象,通常很容易消除此类错误消息.

However, it's usually pretty easy to get rid of such error messages by forcing the objects to be deleted in a different order.

一种(有点奇怪)的方法是重命名一些对象.所以对我来说,如果我简单地将 view 更改为 _view,错误消息就会消失.

One (slightly strange) way to do this is to just rename some of the objects. So for me, the error messages disappear if I simply change view to _view.

然而,也许更好的选择是确保某些关键对象在父/子层次结构中连接在一起:

However, a perhaps better alternative is to make sure that certain key objects are connected together in a parent/child hierarchy:

    view = QGraphicsView()  
    scene = QGraphicsScene(view)

这样做的原因是,当删除一个对象时,Qt 也会自动删除其所有的子节点QObject.这有助于确保在 Python 端之前清理 PyQt 对象的 C++ 端(这确实是导致此类问题的核心).

The reason for doing this, is that when deleteing an object, Qt will also automatically delete all of its descendant QObject nodes. This can help to ensure that the C++ side of PyQt objects is cleaned up before the python side (which is really at the core of what causes these kinds of problems).

另一种可能性是保持对 QApplication 的全局引用,并将其他所有内容放在 main 函数中:

Another possibility is to keep a global reference to the QApplication, and put everything else in a main function:

import sys
from PyQt4.QtGui import *

def main():
    view = QGraphicsView()
    scene = QGraphicsScene()
    scene.addText("Hello!")
    view.setScene(scene)
    view.show()
    return qApp.exec_()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    sys.exit(main())

这篇关于这是 PyQt 4 python 错误还是行为错误的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆