Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't animate QTransform in qgraphicsItem
    primarykey
    data
    text
    <p>I'm trying do some 3D animation in GraphicsScene, for example, to rotate pictures in GraphicsScene (using class, subclassed from <code>qPixmapItem</code> and <code>QObject</code>, if it matters) with Animation framework.</p> <p>Everything works fine, until i want to rotate pictures around vertical axis. There is no way doing so via item.rotate(), so i'm using <code>QTranform</code>.</p> <p>The problem is that doing so does not animate anything at all. What am i doing wrong?</p> <p>P.S. I do not want use OpenGl for this.</p> <p>Here is the way i'm doing it. This way works for animating simpler properties like <code>pos</code>, rotation(via <code>rotation</code>, <code>setRotation</code>) </p> <p>My code :</p> <pre><code>// hybrid graphicsSceneItem, supporting animation class ScenePixmap : public QObject, public QGraphicsPixmapItem { Q_OBJECT Q_PROPERTY(QTransform transform READ transform WRITE setTransform) public: ScenePixmap(const QPixmap &amp;pixmap, QObject* parent = NULL, QGraphicsItem* parentItem = NULL): QObject(parent), QGraphicsPixmapItem(pixmap, parentItem) {} }; </code></pre> <p>Here is how I setup scene and animation:</p> <pre><code>//setup scene //Unrelated stuff, loading pictures, etc. scene = new QGraphicsScene(this); foreach(const QPixmap&amp; image, images) { ScenePixmap* item = new ScenePixmap(image); item-&gt;moveBy(70*i, 0); i++; this-&gt;images.append(item); scene-&gt;addItem(item); } } ui-&gt;graphicsView-&gt;setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern)); ui-&gt;graphicsView-&gt;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-&gt;setDuration(1400); animation-&gt;setStartValue( pixmapItem-&gt;transform()); animation-&gt;setEndValue(getTransform());//here i tried to multiply with default transform , this does not work either return animation; } </code></pre> <p>here is the way i start animation:</p> <pre><code>void MainWindow::keyPressEvent ( QKeyEvent * event ) { if((event-&gt;modifiers() &amp; Qt::ControlModifier)) { QAnimationGroup* groupAnimation = new QParallelAnimationGroup(); foreach(ScenePixmap* image, images) { groupAnimation-&gt;addAnimation( SetupRotationAnimation(image)); } groupAnimation-&gt;start(QAbstractAnimation::DeleteWhenStopped); } } </code></pre> <p>EDIT[Solved] thx to Darko Maksimovic:</p> <p>Here is the code that worked out for me:</p> <pre><code>QGraphicsRotation* getGraphicRotation() { QGraphicsRotation* transform = new QGraphicsRotation(this); transform-&gt;setAxis(Qt::YAxis); return transform; } QAbstractAnimation* SetupRotationAnimation(ScenePixmap* pixmapItem) { QGraphicsRotation* rotation = getGraphicRotation(); QPropertyAnimation* animation = new QPropertyAnimation(rotation, "angle"); animation-&gt;setDuration(1400); animation-&gt;setStartValue( 0); animation-&gt;setEndValue(45); pixmapItem-&gt;setTransformOriginPoint(pixmapItem-&gt;boundingRect().center()); QList&lt;QGraphicsTransform*&gt; transfromations = pixmapItem-&gt;transformations(); transfromations.append(rotation); pixmapItem-&gt;setTransformations(transfromations); return animation; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload