PDA

View Full Version : Animatin a custom property



Luc4
12th April 2010, 15:09
Hi! I created a property in a subclass of QGraphicsProxyWidget this way:


class SlideshowItemProxyWidget : public QGraphicsProxyWidget {
Q_OBJECT
Q_PROPERTY(QPointF center READ center WRITE setCenter)
public:
SlideshowItemProxyWidget(QGraphicsItem* parent);
QPointF center();
void setCenter(QPointF point);
};

SlideshowItemProxyWidget::SlideshowItemProxyWidget (QGraphicsItem* parent) : QGraphicsProxyWidget(parent) {
}

QPointF SlideshowItemProxyWidget::center() {
return QPointF(pos().x() + size().width()/2, pos().y() + size().height()/2);
}

void SlideshowItemProxyWidget::setCenter(QPointF point) {
setPos(point.x() - size().width(), point.y() - size().height());
}

It seems to work correctly when called. The problem is that it seems it's not called when creating an animation with QPropertyAnimation this way:


imageItem = (SlideshowItemProxyWidget*)this->addWidget(labelPixmap);
QPropertyAnimation* animNewImage = new QPropertyAnimation(imageItem, "center");
animNewImage->setDuration(IMAGE_TRANSITION_INTERVAL);
animNewImage->setStartValue(QPointF(1000, 0));
animNewImage->setEndValue(view->mapToScene(QPointF((view->width() - imageItem->boundingRect().width()*currentScale)/2,
(view->height() - imageItem->boundingRect().height()*currentScale)/2).toPoint()));
connect(animNewImage, SIGNAL(finished()), this, SLOT(adaptForNewImage()));
animNewImage->start(QPropertyAnimation::DeleteWhenStopped);

I placed a breakpoint in the method and it doesn't stop there in this case. Nothing happens indeed. Any idea why?
Thanks for any help!

wysota
13th April 2010, 01:09
What is the class of the object represented by "this" in the second snippet? Are you sure its addWidget() method will create a SlideshowItemProxyWidget and not a QGraphicsProxyWidget? A C cast like yours will suceed of course but will not transform a QGraphicsProxyWidget into SlideshowItemProxyWidget. The first method from SlideshowItemProxyWidget you call on it will crash your application. The "center" property will not work either of course. Virtual methods will be called from QGraphicsProxyWidget and not SlideshowItemProxyWidget.

Luc4
13th April 2010, 08:37
You're right! I solved creating the SlideshowItemProxyWidget and then adding it to the scene using the addItem. Thanks for the help!