C11:Implementing Animation

本文详细介绍如何使用Qt进行2D图形显示,包括展示图片、动画球体下落、弹跳球及沿曲线动画等实例,通过具体代码展示了Qt的图形处理能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Displaying a 2D graphical image

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.scene = QGraphicsScene(self)
        pixmap = QtGui.QPixmap()
        pixmap.load("scene.jpg")
        item = QGraphicsPixmapItem(pixmap)
        self.scene.addItem(item)
        self.ui.graphicsView.setScene(self.scene)
if __name__=="__main__":
    app = QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

Making a ball move down on the click of a button

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.startAnimation)
        self.show()
    def startAnimation(self):
        self.anim = QPropertyAnimation(self.ui.label,b"geometry")
        self.anim.setDuration(10000)
        self.anim.setStartValue(QRect(160,70,80,80))
        self.anim.setEndValue(QRect(160,70,220,220))
        self.anim.start()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

Making a bouncing ball

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui =Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.startAnimation)
        self.show()
    def startAnimation(self):
        self.anim = QPropertyAnimation(self.ui.label,b"geometry")
        self.anim.setDuration(10000)
        self.anim.setKeyValueAt(0, QRect(0,0,100,80))
        self.anim.setKeyValueAt(0.5,QRect(160,160,200,180))
        self.anim.setKeyValueAt(1,QRect(400,0,100,80))
        self.anim.start()
if __name__=="__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

Making a ball animate as per the specified curve

Final Result

How to do it?

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.startAnimation)
        self.path = QPainterPath()
        self.path.moveTo(30, 30)
        self.path.cubicTo(30,30,80,180,180,170)
        self.ui.label.pos = QPointF(20,20)
        self.show()
    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        qp.drawPath(self.path)
        qp.end()
    def startAnimation(self):
        self.anim = QPropertyAnimation(self.ui.label,b"pos")
        self.anim.setDuration(4000)
        self.anim.setStartValue(QPointF(20,20))
        positionValues = [n/80 for n in range(0,50)]
        for i in positionValues:
            self.anim.setKeyValueAt(i,self.path.pointAtPercent(i))
            self.anim.setEndValue(QPointF(160,150))
            self.anim.start()
if __name__=="__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值