我正在尝试在PyQt5中设置QGraphicsPixmapItem的动画。 程序按原样崩溃,没有任何错误消息,删除变量“anime”的行可以使程序正常工作。你知道吗
以下是QGraphicsPixMapItem:
class QStone(QGraphicsPixmapItem,QGraphicsObject):
def __init__(self, color, movable):
QGraphicsPixmapItem.__init__(self)
QGraphicsObject.__init__(self)
if movable:
self.setFlag(QGraphicsItem.ItemIsMovable)
white = QPixmap("ressources/white2.png")
black = QPixmap("ressources/black_stone.png")
empty = QPixmap("ressources/no_stone.jpg")
if color == Player.white:
self.setPixmap(white.scaled(60, 60, Qt.KeepAspectRatio))
elif color == Player.black:
self.setPixmap(black.scaled(60, 60, Qt.KeepAspectRatio))
self.w = self.boundingRect().width()
self.h = self.boundingRect().height()
def hoverEnterEvent(self, event):
self.setCursor(Qt.OpenHandCursor)
self.setOpacity(0.5)
event.accept()
def hoverLeaveEvent(self, event):
self.setCursor(Qt.ArrowCursor)
self.setOpacity(1.)
event.accept()
使用QPropertyImation似乎需要QGraphicsObject继承。你知道吗
下面是包含此动画的代码:(此方法属于QGraphicsView的子类):
def display_stone(self, x, y, color=None):
stone = ""
# if color is None:
# stone = QStone("", True)
if color == Player.white:
stone = QStone(Player.white, False)
elif color == Player.black:
stone = QStone(Player.black, False)
stone.setOpacity(0.0)
anime = QPropertyAnimation(stone, b"opacity",self)
anime.setDuration(800)
anime.setStartValue(0.0)
anime.setEndValue(1.0)
anime.start()
stone.setPos(x - stone.w / 2, y - stone.h / 2)
self.scene.addItem(stone)
stone.setZValue(10)
你知道吗? 谢谢
在这种情况下,有以下选项:
1.在本例中,创建一个QObject来处理要修改的属性,该对象由QPropertyImage处理:
2.另一个选项是QVariantAnimation:
(1)https://www.riverbankcomputing.com/static/Docs/PyQt5/qt_interfaces.html
相关问题 更多 >
编程相关推荐