PDA

View Full Version : Constant velocity problem for variable length Text Animation?



ashukla
30th November 2007, 10:28
Dear Everyone!

I am animating text using QTimeline & QGraphicsItem Class. I am facing problem of varing velocity (speed of text) of text if text size is not equal for multiple animation. I want to constant velocity (animation speed constant) for variable length text. What can I do for this.

I am putting the code for that....

QGraphicsTextItem *scrItem = new QGraphicsTextItem(txtScrollEdit->toPlainText());
QDir::setCurrent(QDir::currentPath ()+"/media");
QGraphicsScene *scene=new QGraphicsScene();

scrItem->setDefaultTextColor(Qt::red);
scene->setBackgroundBrush(Qt::yellow);


QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;

animation->setItem(scrItem);


float n;
view=new QGraphicsView(scene,btgScrollText);
view->setWindowFlags (Qt::FramelessWindowHint);

scene->addItem(scrItem);

view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view->setCacheMode(QGraphicsView::CacheBackground);

n=355; /*Width of txtScrollEdit TextEdit*/

qreal h=scene->height()/2-scrItem->boundingRect().height()/2;


register int velocity=40;
register int tl=n*velocity;

timer = new QTimeLine(tl);


timer->setCurveShape(QTimeLine::LinearCurve);
timer->setLoopCount(0);
animation->setTimeLine(timer);
animation1->setTimeLine(timer);


register int scrItembrw=scrItem->boundingRect().width();
for (register int i=0; i<(n+scrItembrw); i=i+1)
{
register QPointF p(0,h); //sets the position of scrItem
p.setX(n-i);
p.setY(h);
animation->setPosAt(i/(n+scrItembrw),p);

}//end for

view->show();
timer->start();
Thanks in Advance!

high_flyer
30th November 2007, 11:03
Your problem is that your animation "frames" number is a function of the text size.
You should have that as a constant, if you want the same speed effect.


register int scrItembrw=scrItem->boundingRect().width();
for (register int i=0; i<(n+scrItembrw) /*this should be a constant*/; i=i+1)
{
register QPointF p(0,h); //sets the position of scrItem
p.setX(n-i);
p.setY(h);
animation->setPosAt(i/(n+scrItembrw),p);

}//end for