如果你的 QGraphicsView 中的 item 数目是不定的,你可以使用 QGraphicsScene 来动态添加和删除 item。下面是一个简单的例子:

from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem
from PyQt5.QtCore import Qt

# 创建 QGraphicsScene 和 QGraphicsView 对象
scene = QGraphicsScene()
view = QGraphicsView(scene)

# 添加一个 QGraphicsEllipseItem
ellipse = QGraphicsEllipseItem(0, 0, 100, 100)
ellipse.setBrush(Qt.red)
scene.addItem(ellipse)

# 动态添加其他 QGraphicsEllipseItem
for i in range(5):
    new_ellipse = QGraphicsEllipseItem(50*i, 50*i, 50, 50)
    new_ellipse.setBrush(Qt.blue)
    scene.addItem(new_ellipse)

# 显示 QGraphicsView
view.show()

在上面的代码中,我们创建了一个 QGraphicsScene 对象,并使用 addItem() 方法向场景中添加了一个 QGraphicsEllipseItem。然后,我们使用一个循环动态地添加了其他的 QGraphicsEllipseItem。最后,我们显示了 QGraphicsView 对象,即可看到我们创建的动态 item


原文地址: https://www.cveoy.top/t/topic/dUco 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录