这个世界上只有GISer伤得起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以用一个矩形进行演示。首先,我们需要创建一个QWidget类的子类,并在其paintEvent()函数中绘制一个矩形: ```python class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setGeometry(100, 100, 300, 200) self.pos_x = 0 self.pos_y = 0 def paintEvent(self, event): painter = QPainter(self) painter.fillRect(QRect(self.pos_x, self.pos_y, 50, 50), QColor(255, 0, 0)) ``` 在这个类中,我们定义了一个矩形的左上角坐标pos_x和pos_y,并在paintEvent()函数中使用QPainter绘制出来。接下来,我们可以使用QPropertyAnimation对其位置进行动画处理。我们在MyWidget类中添加一个按钮,点击按钮时启动动画,代码如下: ```python class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setGeometry(100, 100, 300, 200) self.pos_x = 0 self.pos_y = 0 # 添加按钮并连接槽函数 self.button = QPushButton(self) self.button.setText("Start Animation") self.button.clicked.connect(self.start_animation) def paintEvent(self, event): painter = QPainter(self) painter.fillRect(QRect(self.pos_x, self.pos_y, 50, 50), QColor(255, 0, 0)) def start_animation(self): animation = QPropertyAnimation(self, b"pos") animation.setDuration(1000) animation.setStartValue(QPoint(0, 0)) animation.setEndValue(QPoint(250, 150)) animation.start() ``` 在start_animation()函数中,我们使用QPropertyAnimation对MyWidget的位置属性进行动画处理。动画的起始值为(0,0),结束值为(250,150),动画时间为1秒。在按钮点击事件中启动该动画。 运行程序,点击按钮即可看到矩形在窗口中移动的动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值