gpt4 book ai didi

python - 从其中一项的事件中清除 QGraphicsScene

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:40 26 4
gpt4 key购买 nike

我有一个带有许多 QGraphicsItem 的 QGraphicsScene。有些项目具有清除和重新绘制场景的按钮。

The problem is that the clear() method deletes the QButton (and its associated data structures) in the middle of a method call that uses those very data structures. Then, immediately after clear() returns, the calling method tries to access the now-deleted data (because it wasn't expecting to be deleted in the middle of its routine), and bang -- a crash. From here.

我找到了C++的解决方案here ,但是我使用的是 PySide,无法对 python 使用相同的解决方案。

按照我的代码操作:

class GraphicsComponentButtonItem(QtGui.QGraphicsItem):

def __init__(self, x, y, update_out):
super(GraphicsComponentButtonItem, self).__init__()

self.x = x
self.y = y
self.update_out = update_out

self.setPos(x, y)

self.createButton()
self.proxy = QtGui.QGraphicsProxyWidget(self)
self.proxy.setWidget(self.button)

def boundingRect(self):
return QtCore.QRectF(self.x, self.y, self.button.width(), self.button.height())

def paint(self, painter, option, widget):
# Paint same stuffs

def createButton(self):
self.button = QtGui.QPushButton()
self.button.setText('Clear')
self.button.clicked.connect(self.action_press)

def action_press(self):
# Run some things
self.update_out()


class QGraphicsViewButtons(QtGui.QGraphicsView):

def __init__(self, scene, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene = scene

# It's called outside
def updateScene(self):
self.scene.clear()
self.scene.addItem(GraphicsComponentButtonItem(0, 0, self.updateScene))
self.scene.addItem(GraphicsComponentButtonItem(0, 50, self.updateScene))
self.scene.addItem(GraphicsComponentButtonItem(0, 100, self.updateScene))

最佳答案

以下C++代码的转换:

QObject::connect(button, SIGNAL(clicked()), scene, SLOT(clear()), Qt::QueuedConnection);

Python 是:

self.button.clicked.connect(self.scene().clear, QtCore.Qt.QueuedConnection)

关于python - 从其中一项的事件中清除 QGraphicsScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48387136/

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