PDA

View Full Version : Mapping QGraphicsItem objects



ashukla
3rd February 2008, 13:46
Dear All!

QGraphicsTextItem *scrItem = new QGraphicsTextItem("WELCOME MY SIR");
scrItem->setDocument(txt->document());
QDir::setCurrent(QDir::currentPath ()+"/media");

QPixmap pico("rose.jpg");
pico=pico.scaled ( t.Font.pointSize(),t.Font.pointSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));

QGraphicsItem *ship=new MyPixmapItem(pico,0,1);


scrItem->setFont(t.Font);

QGraphicsItemGroup *gpItem=new QGraphicsItemGroup;
gpItem->addToGroup (ship);
gpItem->addToGroup (scrItem);
gpItem->addToGroup (scrItem1);

QGraphicsScene *scene=new QGraphicsScene();

QPointF m(scrItem->mapToItem (ship,QPointF(ship->boundingRect().width(),0)));
qDebug()<<"Before\tText Item POS = "<<scrItem->scenePos()<<" Image Item POS = "<<ship->scenePos();

scrItem->changeGeometry();
QPointF sf=scrItem->mapFromScene(scrItem->mapToScene(m));
qDebug()<<sf;
qDebug()<<"After\tText Item POS = "<<scrItem->scenePos()<<" Image Item POS = "<<ship->scenePos()<<"\n";
view=new QGraphicsView(scene,btgScrollText);
scene->addItem(gpItem);
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(gpItem);

I want to put scrItem after ship object. How to map this.

Gopala Krishna
3rd February 2008, 15:00
...
QGraphicsItemGroup *gpItem=new QGraphicsItemGroup;
gpItem->addToGroup (ship);
gpItem->addToGroup (scrItem);
gpItem->addToGroup (scrItem1);

QPointF right(ship->boundingRect().right(), 0);

scrItem->setPos(ship->mapToParent(right)); //this should do the trick.
...

But there is a subtle problem here. Moving the items individually after inserting into a group doesn't update group's geometry. So there are two solutions for this
1) Add all the items to scene first, position them as needed and then add to group.
2) Otherwise preserve what you are doing, but after that add a dummy item and remove it from scene to update group's geometry.

I think there is fault in your code. You are trying to map points to/from scene before adding those items to scene.

Hope this helps :)

ashukla
4th February 2008, 05:45
Dear Gopala Krishna!
Thanks! for your valuable response.
Except setPos(), is any function exists to set position of items in a scene & view.

aamer4yu
4th February 2008, 11:14
and why would you need it ? to set positions of objects in Scene ? setPos is meant for that.

Also if you are confused between the mapToParent,mapToscene, etc,,,,, use item->scenePos() function.
rest gopal has given correct answer i guess for the details u gave.

Gopala Krishna
4th February 2008, 14:47
Dear Gopala Krishna!
Thanks! for your valuable response.
Except setPos(), is any function exists to set position of items in a scene & view.

Hmm, i guess no.. Note that setPos sets item's position in parent's coordinates. Only if it doesn't have parent setPos sets item's pos in scene coordinates.
Source : QGraphicsItem::setPos