gpt4 book ai didi

python - 异步添加对象到 QGraphicsScene

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:56 25 4
gpt4 key购买 nike

我开发了一个应用程序,其中需要将许多对象添加到 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_())

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

由于有人要求我再举一个例子,这里是完整的例子:

import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from GridCal.Gui.GridEditorWidget import GridEditor
from GridCal.Engine.IO.file_handler import FileOpen


class AddObjectsThreaded(QThread):

def __init__(self, editor: GridEditor, explode_factor=1.0):
QThread.__init__(self)

self.editor = editor

self.explode_factor = explode_factor

def run(self):
"""
run the file open procedure
"""
# clear all
self.editor.diagramView.scene_.clear()

# first create the buses
for bus in self.editor.circuit.buses:
bus.graphic_obj = self.editor.add_api_bus(bus, self.explode_factor)

for branch in self.editor.circuit.branches:
branch.graphic_obj = self.editor.add_api_branch(branch)

# figure limits
min_x = sys.maxsize
min_y = sys.maxsize
max_x = -sys.maxsize
max_y = -sys.maxsize

# Align lines
for bus in self.editor.circuit.buses:
bus.graphic_obj.arrange_children()
# get the item position
x = bus.graphic_obj.pos().x()
y = bus.graphic_obj.pos().y()

# compute the boundaries of the grid
max_x = max(max_x, x)
min_x = min(min_x, x)
max_y = max(max_y, y)
min_y = min(min_y, y)

# set the figure limits
self.editor.set_limits(min_x, max_x, min_y, max_y)
# center the view
self.editor.center_nodes()


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

fname = r'1354 Pegase.xlsx' # this file you need to download from https://github.com/SanPen/GridCal/tree/master/Grids_and_profiles/grids
circuit = FileOpen(fname).open()

view = GridEditor(circuit)
view.resize(600, 400)
view.show()

thr = AddObjectsThreaded(editor=view)
thr.start()

sys.exit(app.exec_())

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

Process finished with exit code -1073740791 (0xC0000409)

最佳答案

根据其他类似问题,解决方案可能是重新安装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.

关于python - 异步添加对象到 QGraphicsScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58085602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com