PDA

View Full Version : Removing a QGraphicsPixmapItem from a scene



newqtuser
12th July 2008, 07:52
Hi folks

Ok im wondering if anyone out there can give me an idea on how i can remove a QGraphicsPixmapItem from my scene after the animation is complete. I need to move a few pixmaps across the scene from left to right and have only been doing qt4 for about a month still new to this. Anyhow so far i have an image that can scroll across then after it reaches the right side and is no longer visable i need to call removeItem() but im not sure what to check to see where this item is on the scene, i want it to look like it scrolled off the scene. Also i may have more then 1 item on the scene so thats why i used QGraphicsItemAnimation in case i need to speed or slow down items. Here is some test code, just replace the testimage with any jpg. Any help would be appreciated. Here is the code, very short.


#include <QtGui>

int main (int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap testimage("/images/car.jpg");
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(testimage);
QTimeLine *timer = new QTimeLine(15000);

timer->setFrameRange(0, 100);
timer->setLoopCount(1);

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

animation->setPosAt(0.0, QPoint(-800, 100));
animation->setPosAt(0.10, QPoint(-600, 100));
animation->setPosAt(0.15, QPoint(-400, 100));
animation->setPosAt(0.20, QPoint(-100, 100));
animation->setPosAt(0.25, QPoint(-50, 100));
animation->setPosAt(0.50, QPoint(600, 100));

QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 250, 250);
scene->addItem(item);

QGraphicsView *view = new QGraphicsView(scene);
view->resize(1024, 768);
view->show();

timer->start();
return a.exec();
}

caduel
12th July 2008, 10:15
you can check in the QGraphicsScene::sceneRect() QRectF::contain()s the QGraphicsPixmapItem::pos()

HTH

newqtuser
12th July 2008, 23:54
Ok im going to give that a shot, thanks