PDA

View Full Version : How to set definite Scene Rect in QGraphicsView.



AmolShinde_8
9th October 2008, 15:20
Hi All,
I am developing a application in which I am adding no of items into the scene. I know that by default view shows the item in the center. thats fine with my application. Now when I try to animate the items with setPosAt to move the items all the items gets togather at the center of the view overlapping eachother. I think this is the problem with SceneRect. Now I need to set the definitive scene rect . I have tried it to set using setSceneRect but it doesn't worked. Can anyone please help me out. Do I need to subclass QGraphicsScene class inorder to override setSceneRect.

Thanks and Advance.

aamer4yu
9th October 2008, 15:45
I dont think its prob with scene rect.
You might be making some mistake while placing the items. Can we see what u are trying ??

AmolShinde_8
9th October 2008, 16:18
Thanks for your reply..
Here is the code.. Can you please tell me how can I scale the two rects at the center.


#include <QtGui/QApplication>
#include "animation.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItemAnimation>
#include <QGraphicsEllipseItem>
#include <QTimeLine>
#include "windows.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//Animation w;
//w.show();

QGraphicsRectItem *ball = new QGraphicsRectItem(150, 150, 100, 100);
ball->setBrush(QBrush(QColor(255,0,0)));
QGraphicsEllipseItem *ecl = new QGraphicsEllipseItem(150, 150, 20, 20);
ecl->setBrush(QBrush(QColor(0,255,0)));
QGraphicsRectItem *Rect = new QGraphicsRectItem(200, 175, 100, 50);
Rect->setBrush(QBrush(QColor(0,0,255)));
Rect->setZValue(-1);

QTimeLine *timer = new QTimeLine(15000);
timer->setFrameRange(0, 100);

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(ball);
animation->setTimeLine(timer);

QGraphicsItemAnimation *animation1 = new QGraphicsItemAnimation;
animation1->setItem(Rect);
animation1->setTimeLine(timer);


QGraphicsScene *scene = new QGraphicsScene();
scene->addItem(ball);
//scene->addItem(ecl);
scene->addItem(Rect);


QGraphicsView *view = new QGraphicsView(scene);
view->setAlignment(Qt::AlignCenter);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
view->setGeometry(10,10,900,700);
view->show();

//timer->start();

QRectF rc = ball->mapToScene(ball->boundingRect()).boundingRect();
QRectF rc1 = ecl->mapToScene(ecl->boundingRect()).boundingRect();
QRectF rc2 = Rect->mapToScene(Rect->boundingRect()).boundingRect();

for (int i = 0; i < 200; ++i)
{
//animation->setTranslationAt(i/200.0, rc.width()/2.0,rc.height()/2.0);
animation->setScaleAt(i/200.0, 1 + i/200.0, 1 + i/200.0);
//animation->setTranslationAt(i/200.0, -( rc.width()/2.0 + i/200.0), -rc.height()/2.0);
animation->setPosAt(i/200.0, QPointF(150.0 - i/200.0, rc.top()));
}

for (int i = 0; i < 200; ++i)
{
//animation1->setTranslationAt(i/200.0, -i/200.0,0);
animation1->setScaleAt(i/200.0, 1 + i/200.0, 1 + i/200.0);
//animation1->setTranslationAt(i/200.0, -rc2.width()/2.0, -rc2.height()/2.0);
animation1->setPosAt(i/200.0, QPointF(150.0 -i/200.0, rc2.top()));
}


rc = ball->mapToScene(ball->boundingRect()).boundingRect();
rc1 = ecl->mapToScene(ecl->boundingRect()).boundingRect();
rc2 = Rect->mapToScene(Rect->boundingRect()).boundingRect();


//scene->addItem(Rect);

//scene->setSceneRect(10, 10, 900, 700);

return a.exec();
}