gpt4 book ai didi

c++ - 无法在 qgraphicsItem 中为 QTransform 设置动画

转载 作者:行者123 更新时间:2023-11-28 03:37:04 26 4
gpt4 key购买 nike

我正在尝试在 GraphicsScene 中做一些 3D 动画,例如,在 GraphicsScene 中旋转图片(使用类,从 qPixmapItemQObject 子类,如果重要的话)带有动画框架。

一切正常,直到我想绕垂直轴旋转图片。无法通过 item.rotate() 执行此操作,因此我使用的是 QTranform

问题是这样做根本不会激活任何东西。我做错了什么?

附言我不想为此使用 OpenGl。

这是我正在做的方式。这种方式适用于为更简单的属性设置动画,例如 pos、旋转(通过 rotationsetRotation)

我的代码:

// hybrid graphicsSceneItem, supporting animation 
class ScenePixmap : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY(QTransform transform READ transform WRITE setTransform)
public:
ScenePixmap(const QPixmap &pixmap, QObject* parent = NULL, QGraphicsItem* parentItem = NULL):
QObject(parent),
QGraphicsPixmapItem(pixmap, parentItem)

{}
};

下面是我如何设置场景和动画:

//setup scene
//Unrelated stuff, loading pictures, etc.
scene = new QGraphicsScene(this);
foreach(const QPixmap& image, images)
{
ScenePixmap* item = new ScenePixmap(image);
item->moveBy(70*i, 0);
i++;
this->images.append(item);
scene->addItem(item);
}
}

ui->graphicsView->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
ui->graphicsView->setScene(scene);

//setup animation
QTransform getTransform()
{
QTransform transform;
transform.rotate(-30, Qt::ZAxis);//also tried transform = transform.rotate(...)

return transform;
}

QAbstractAnimation* SetupRotationAnimation(ScenePixmap* pixmapItem)
{
QPropertyAnimation* animation = new QPropertyAnimation(pixmapItem, "transform");
animation->setDuration(1400);
animation->setStartValue( pixmapItem->transform());
animation->setEndValue(getTransform());//here i tried to multiply with default transform , this does not work either
return animation;
}

这是我开始动画的方式:

void MainWindow::keyPressEvent ( QKeyEvent * event )
{

if((event->modifiers() & Qt::ControlModifier))
{
QAnimationGroup* groupAnimation = new QParallelAnimationGroup();


foreach(ScenePixmap* image, images)
{
groupAnimation->addAnimation( SetupRotationAnimation(image));

}

groupAnimation->start(QAbstractAnimation::DeleteWhenStopped);

}
}

编辑 [已解决] 感谢 Darko Maksimovic:

这是对我有用的代码:

QGraphicsRotation* getGraphicRotation()
{
QGraphicsRotation* transform = new QGraphicsRotation(this);

transform->setAxis(Qt::YAxis);

return transform;
}

QAbstractAnimation* SetupRotationAnimation(ScenePixmap* pixmapItem)
{
QGraphicsRotation* rotation = getGraphicRotation();

QPropertyAnimation* animation = new QPropertyAnimation(rotation, "angle");
animation->setDuration(1400);
animation->setStartValue( 0);
animation->setEndValue(45);

pixmapItem->setTransformOriginPoint(pixmapItem->boundingRect().center());

QList<QGraphicsTransform*> transfromations = pixmapItem->transformations();
transfromations.append(rotation);
pixmapItem->setTransformations(transfromations);

return animation;
}

最佳答案

我看到你使用 QTransform。如果您只需要一次旋转,即简单的旋转,最好使用 setRotation [不要忘记 setTransformOriginPoint]。

但是,如果你想记住许多旋转,例如围绕不同的变换点,那么你应该使用 QGraphicsTransform,即它的专用派生类 QGraphicsRotation,它您通过在图形对象上调用 setTransformations 来应用(您应该首先通过调用 o.transformations() 获取现有的转换,附加到此,然后调用 setTransformations;如果您保留指向添加的转换的指针,您以后也可以直接改)。

从您发布的代码中我看不出您的错误从何而来,但是通过使用专门的函数您可以避免一些常见的问题。

附言我还看到您在发布的代码中没有使用 prepareGeometryChange,因此请注意,在转换对象时这是必要的。

关于c++ - 无法在 qgraphicsItem 中为 QTransform 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com