我正在尝试创建自己的图形项目,其中心有一个圆圈及其标签。
class circle(QGraphicsItem):
def __init__(self, radius=None, name=None, x=None, y=None, parent=None):
super(circle, self).__init__(parent)
self.radius = radius if radius else random.random()*500
self.label = name if name else "cirA"
self.x = x if x else random.randint(0, 900)
self.y = y if y else random.randint(0, 600)
# self.center = complex(self.x, self.y)
def boundingRect(self):
penWidth = 1.0
return QRectF(-self.x - penWidth / 2, -self.y - penWidth / 2,
self.x + penWidth, self.y + penWidth)
def paint(self, painter, option, widget):
painter.drawEllipse(0, 0, self.radius, self.radius)
painter.drawText(self.label)
现在在我的主GUI中,一个圆圈列表被保存为circleList,我正在尝试将圆圈项目添加为
for cir in self.circleList:
self.painter.addItem(cir)
但这会返回
RuntimeError: wrapped C/C++ object of type circle has been deleted
帮助?
这应该可以完成最小可复制示例的技巧
注意:FBS是必需的,项目应使用
fbs startproject
该圆圈也将与以下内容一起添加
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class gui(QDialog):
def __init__(self, parent=None):
super(gui, self).__init__(parent)
self.painter = QGraphicsScene(10, 10, 900, 600)
self.canvas = QGraphicsView(self.painter)
mainLayout = QGridLayout()
mainLayout.addWidget(self.canvas, 0, 0, 6, 2)
self.setLayout(mainLayout)
self.circleList = []
def drawCircle(self):
pen = QPen(Qt.black, 2, Qt.SolidLine)
self.painter.clear()
for cir in self.circleList:
self.painter.addItem(cir)
self.painter.update()
self.canvas.update()
self.update()
def newCircle(self, cir):
self.circleList.append(cir)
self.drawCircle()
def addCircle(self):
return self.newCircle(circle())
开满天机
相关分类