我是Qt的新手,我在QWidget的旋转方面遇到了一些问题。
我在QLabel里面有一个QPixmap。我想要的是用一个连续旋转90度的动画。。
我知道QPropertyAnimation,我知道如何使用它,但是我很难用它来旋转一个QWidget。有什么简单的方法可以实现我的目标,用动画旋转整个QLabel或QPixmap吗?谢谢。
发布于 2018-05-11 08:09:37
这是QLabel/QPixmap随动画旋转的演示。没有必要使用QPropertyAnimation。因为QLabel或QPixmap没有旋转属性。因此,使用QVariantAnimation使QPixmap旋转为动画,并使用QPixmap::转换旋转它。如果您想很好地控制像素地图的动画,强烈建议使用QGraphicsPixmapItem和QPropertyAnimation。
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;
};发布于 2018-05-11 07:23:14
可以通过两种方式实现轮换:
1)创建一个静态图像集合,每个图像都表示按某个角度旋转的原始像素图。使用计时器,您可以从集合中用一个更改标签的像素映射。这将模仿动画旋转。
2)使用单个像素映射,重写标签的QLabel::painEvent(),每次重绘标签时,都应该旋转带有QPainter::rotate()函数的QPainter对象。
https://stackoverflow.com/questions/50286919
复制相似问题