QTPropertyImation仅更改

2024-05-01 07:54:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用animator类生成ellipseobject并向其添加动画,目标只是更改圆点的比例(从两倍大小到正常大小),而不是进行平移。现在我面对的是圆点确实缩小了,但它从(xy值是其原始xy值的两倍)位置移动到了原始xy位置

这是动画师课程

class Animator:
    def __init__(self, animation_config, num_rows, num_cols, background_colour, parent, user_idx = 0, resolution_width = 1920,
                  resolution_height = 1200, log_file_path = "coordinates.txt"):
        with open(log_file_path, "w") as log_file:
            log_file.write("")

        circle = animation_config["circle"]
        self.__cur_circle = None
        self.__cur_colour = None
        self.__animated_colour = QColor(*circle["colour"])
        self.__log_file_path = log_file_path

        # Initialize the circle table

        diamond_square = DiamondSquare()
        self.__user_idx = user_idx % 64
        shift_xn = diamond_square.dx[user_idx]
        shift_yn = diamond_square.dy[user_idx]
        intv_x = (resolution_width - 20) / 10
        intv_y = (resolution_height - 20) / 10
        print(shift_xn,shift_yn)
        self.__circle_table = []
        y = 0
        for i in range (11):
            circles = []
            x = 0
            for j in range (11):
                cir_x = (j * intv_x) + shift_xn
                cir_y = (i * intv_y) + shift_yn
                print(cir_x,cir_y)
                if cir_x <= resolution_width and cir_y <= resolution_height:
                    circles.append(EllipseObject(parent,cir_x,cir_y, x, y, intv_x, intv_y,
                                                 hidden_colour = background_colour,
                                                 display_colour=self.__animated_colour))
                    x += 1
            y += 1

            self.__circle_table.append(circles)

        # Initalize the first animation
        self.__first = QPropertyAnimation()
        self.__first.setPropertyName(b"scale")
        self.__first.setDuration(animation_config["animation_duration"])
        self.__first.setStartValue(2)
        self.__first.setEndValue(1)

这是我的EllipseObject课程

class EllipseObject(QGraphicsWidget):
    def __init__(
        self,
        parent,
        x = 0,
        y = 0,
        ind_x = 0,
        ind_y = 0,
        intv_x = 0,
        intv_y = 0,
        width = 20,
        height = 20,
        hidden = True,
        hidden_colour = QColor(Qt.white),
        display_colour = QColor(Qt.black)
        ):

        super().__init__(parent)
        self.__x = x
        self.__y = y
        self.__ind_x = ind_x
        self.__ind_y = ind_y
        self.__intv_x = intv_x
        self.__intv_y = intv_y
        self.__height = height
        self.__width = width
        self.hidden = hidden
        self.hidden_colour = hidden_colour
        self.display_colour = display_colour

    def paint(self, painter, option, widget = None):
        colour = QColor(Qt.white) if self.hidden else self.display_colour
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(colour)
        painter.setBrush(QBrush(colour))
        # painter.drawEllipse(self.__x, self.__y, self.__width, self.__height)
        painter.drawEllipse(self.boundingRect())
        # point = self.mapToScene(self.boundingRect().center())
        # print("Draw this ",point.x(), point.y())

    def get_coordinates(self):
        point = self.mapToScene(self.boundingRect().center())
        return (point.x(), point.y())

    def boundingRect(self):
        return QRectF(self.__x,
                      self.__y,
                      self.__height, self.__width)

Tags: selflogshiftwidthhiddenfilefirstheight
1条回答
网友
1楼 · 发布于 2024-05-01 07:54:46

你可能观察到的是,它相对于左上角上升,并且会给你一个它正在移动的外观。QGraphicsItem有一个名为transformOriginPoint的属性,对该属性执行旋转和缩放等转换,并在点(0,0)处找到该属性。在您的代码中有几个错误,您必须使用setPos()而不是通过boundingRect()设置位置,以便缩放是相对于项目中心的,从而确定boundingRect()是对称的

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class EllipseObject(QGraphicsWidget):
    def __init__(
        self,
        parent=None,
        x = 0,
        y = 0,
        ind_x = 0,
        ind_y = 0,
        intv_x = 0,
        intv_y = 0,
        width = 20,
        height = 20,
        hidden = True,
        hidden_colour = QColor(Qt.white),
        display_colour = QColor(Qt.black)
        ):

        super().__init__(parent)
        self.__x = x
        self.__y = y
        self.__ind_x = ind_x
        self.__ind_y = ind_y
        self.__intv_x = intv_x
        self.__intv_y = intv_y
        self.__height = height
        self.__width = width
        self.hidden = hidden
        self.hidden_colour = hidden_colour
        self.display_colour = display_colour
        self.setPos(self.__x, self.__y)

    def paint(self, painter, option, widget = None):
        colour = QColor(Qt.white) if self.hidden else self.display_colour
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(colour)
        painter.setBrush(QBrush(colour))
        # painter.drawEllipse(self.__x, self.__y, self.__width, self.__height)
        painter.drawEllipse(self.boundingRect())

    def get_coordinates(self):
        return (self.pos().x(), self.pos().y())

    def boundingRect(self):
        return QRectF(-0.5*self.__width,
                      -0.5*self.__height,
                      self.__height, self.__width)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = QGraphicsView()
    scene = QGraphicsScene(w)
    w.setScene(scene)
    it = EllipseObject(x=100, y=100, hidden=False)
    scene.addItem(it)

    animation = QPropertyAnimation(it, b"scale")
    animation.setDuration(2000)
    animation.setStartValue(2)
    animation.setEndValue(1)
    animation.start()
    w.show()
    sys.exit(app.exec_())

注:

与其使用名为hidden的标志,不如直接使用setVisible(boolean)show()hide()。Qt已经实现了许多方法,轮子并没有重新发明

相关问题 更多 >