将对象异步添加到qgraphicscen

2024-05-01 00:02:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我开发了一个应用程序,其中需要将许多对象添加到QGraphicsScene对象中,以表示电力系统的图。当网格很大时,这个过程需要一段时间,主窗口仍然没有响应。因此,我想知道一种异步创建对象的方法。

下面的代码是actual code的一个非常简化的版本。在本例中,我创建了对象,并使用函数add_objects将它们添加到scene。这是应该在线程中运行的函数。在

import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *


def add_objects(scene: QGraphicsScene, n=1000):
    for i in range(n):
        item = QGraphicsEllipseItem(i * 5, 10, 60, 40)
        scene.addItem(item)


class MyView(QGraphicsView):
    def __init__(self):
        QGraphicsView.__init__(self)

        self.setGeometry(QRect(100, 100, 600, 250))

        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(QRectF())

        self.setScene(self.scene)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = MyView()
    view.show()
    add_objects(scene=view.scene, n=100)
    sys.exit(app.exec_())

-----编辑--------

既然有人问我另一个例子,下面是完整的案例:

^{pr2}$

此进程完全崩溃,并出现以下错误:

Process finished with exit code -1073740791 (0xC0000409)


Tags: 对象函数fromimportselfviewaddobjects
1条回答
网友
1楼 · 发布于 2024-05-01 00:02:40

根据其他类似的问题,解决方案可以是重新安装pyqt,请参见here。这些答案意味着pyqt的过时版本是导致此问题的原因。在您的例子中,由于您使用的是PySide2,所以我建议您删除当前的任何版本,然后重新安装,如果您使用的是pip:

pip uninstall PySide2
pip install  upgrade  no-cache-dir PySide2

如果这没有帮助,我也在一个问题中发现了另一个类似的错误,我希望它至少能让你在正确的direction中找到答案(这可能已经在注释中提到了,不要从其他线程添加UI元素),我把它复制到这里:

STATUS_STACK_BUFFER_OVERRUN is a /GS exception. They are thrown when Windows detects 'tampering' of a security cookie protecting a return address. It is probable that you are writing something past the end of a buffer, or writing something to a pointer that is pointing to the wrong place. However it is also possible that you have some dodgy memory or otherwise faulty hardware that is tripping validation code.

One thing that you could try is to disable the /GS switch (project properties, look for C/C++ -> Code Generation -> Buffer Security Check) and recompile. Running the code again may well cause an error that you can trap and trace. I think /GS is designed not to give you any info for security reasons.

Another thing you could do is run the code as is on a different PC and see if that fails, this may point to a hardware problem if it doesn't.

相关问题 更多 >