我正在尝试为PyQt5中的QGraphicsPixmapItem设置动画。
程序按原样崩溃,没有任何错误消息,而删除有关变量“动漫”的行可使程序正常运行。

这是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()


使用QPropertyAnimation似乎需要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)


任何想法?
谢谢

最佳答案

与Qt C ++ API不同,PyQt不允许双重继承(在特殊情况下(1)除外),因此您无法实现从QGraphicsPixmapItem和QGraphicsObject继承的类。

在这种情况下,有以下选项:

1.在这种情况下,将创建一个QObject来处理要修改的属性,该对象是由QPropertyAnimation处理的对象:

class OpacityManager(QObject):
    opacityChanged = pyqtSignal(float)

    def __init__(self, initial_opacity, parent=None):
        super(OpacityManager, self).__init__(parent)
        self._opacity = initial_opacity

    @pyqtProperty(float, notify=opacityChanged)
    def opacity(self):
        return self._opacity

    @opacity.setter
    def opacity(self, v):
        if self._opacity != v:
            self._opacity = v
            self.opacityChanged.emit(self._opacity)


class QStone(QGraphicsPixmapItem):
    def __init__(self, color, movable=False):
        QGraphicsPixmapItem.__init__(self)

        self.manager = OpacityManager(self.opacity())
        self.manager.opacityChanged.connect(self.setOpacity)

        if movable:
            self.setFlag(QGraphicsItem.ItemIsMovable)
        # ...


# ...
anime = QPropertyAnimation(stone.manager, b"opacity", stone.manager)
# ...


2.另一个选项是QVariantAnimation:

# ...
anime = QVariantAnimation(self)
anime.valueChanged.connect(stone.setOpacity)
anime.setDuration(800)
# ...


(1)https://www.riverbankcomputing.com/static/Docs/PyQt5/qt_interfaces.html

09-26 16:11