我有一个自定义的“加载”图像,我需要能够在某些动作发生时启动/停止旋转动画。我正在寻找代码,将告诉我如何做到这一点,但我找不到任何具体的旋转PyQt。我已经有了以下代码,它可以使图像在单击按钮时旋转:
import os
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class myApplication(QWidget):
def __init__(self, parent=None):
super(myApplication, self).__init__(parent)
self.imgPath = os.path.join('path/to/image','image.svg')
pixmap = QPixmap(self.imgPath)
diag = (pixmap.width()**2 + pixmap.height()**2)**0.5
self.label = QLabel()
self.label.setMinimumSize(diag, diag)
self.label.setAlignment(Qt.AlignCenter)
self.label.setPixmap(pixmap)
#---- Prepare a Layout ----
grid = QGridLayout()
button = QPushButton('Rotate 15 degrees')
button.clicked.connect(self.rotate_pixmap)
grid.addWidget(self.label, 0, 0)
grid.addWidget(button, 1, 0)
self.setLayout(grid)
self.rotation = 0
def rotate_pixmap(self):
pixmap = QPixmap(self.imgPath)
self.rotation += 15
transform = QTransform().rotate(self.rotation)
pixmap = pixmap.transformed(transform, Qt.SmoothTransformation)
#---- update label ----
self.label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
instance = myApplication()
instance.show()
sys.exit(app.exec_())
我找到了this C++ code,它显示了如何使用QVariantAnimation
和QTransform
创建旋转动画:
class RotateMe : public QLabel {
Q_OBJECT
public:
explicit RotateMe(QWidget* parent = Q_NULLPTR) :
QLabel(parent),
pixmap(100, 100),
animation(new QVariantAnimation )
{
resize(200, 200);
pixmap.fill(Qt::red);
animation->setDuration(10000);
animation->setStartValue(0.0f);
animation->setEndValue(90.0f);
connect(animation, &QVariantAnimation::valueChanged, [=](const QVariant &value){
qDebug()<<value;
QTransform t;
t.rotate(value.toReal());
setPixmap(pixmap.transformed(t));
});
animation->start();
}
private:
QPixmap pixmap;
QVariantAnimation *animation;
};
如何用python重新编写?我找不到任何例子来说明QVariantAnimation
是如何在PyQT中使用的
逻辑类似于我在下面展示的:
相关问题 更多 >
编程相关推荐