我有一个自定义的 "加载 "图像,我需要能够在一些动作发生时开始停止旋转动画。我正在寻找能告诉我如何做到这一点的代码,但我在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_())
我发现 这段C++代码 其中展示了如何使用 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中使用。
逻辑是类似的,我下面显示。
import os
from PyQt5 import QtCore, QtGui, QtWidgets
class RotateMe(QtWidgets.QLabel):
def __init__(self, *args, **kwargs):
super(RotateMe, self).__init__(*args, **kwargs)
self._pixmap = QtGui.QPixmap()
self._animation = QtCore.QVariantAnimation(
self,
startValue=0.0,
endValue=360.0,
duration=1000,
valueChanged=self.on_valueChanged
)
def set_pixmap(self, pixmap):
self._pixmap = pixmap
self.setPixmap(self._pixmap)
def start_animation(self):
if self._animation.state() != QtCore.QAbstractAnimation.Running:
self._animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
def on_valueChanged(self, value):
t = QtGui.QTransform()
t.rotate(value)
self.setPixmap(self._pixmap.transformed(t))
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
label = RotateMe(alignment=QtCore.Qt.AlignCenter)
img_path = os.path.join('path/to/image','image.svg')
label.set_pixmap(QtGui.QPixmap(img_path))
button = QtWidgets.QPushButton('Rotate')
button.clicked.connect(label.start_animation)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(label)
lay.addWidget(button)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())