PDA

View Full Version : ScenePos changes when rotating an item



jano_alex_es
22nd October 2009, 16:26
Hi,

I have a QGraphicsPixmapItem on an Scene, I need to rotate it.


float xx= this->pixmap().width() / 2;
float yy= this->pixmap().height() / 2;
setTransform(QTransform().translate(xx,yy).rotate( m_iAngle).translate(-xx, -yy));


My application has the option to export the ScenePos of the item into an XML file.

Everything goes right if I don't rotate the item, but if we have an item, we rotate it, we write its scenePos in the xml file and we read the xml file its position on the screen has changed.

My app first reads the xml file, then it creates a new item, it sets the pos and rotate later, but if I create it first, rotate it later and after all set the pos the result is the same :S The item moves itself magically .

wysota
22nd October 2009, 16:35
The position of the pixmap item corresponds to its upper left corner. As you rotate the item not around its origin but around its middle point, the scenePos changes as the upper left corner of the pixmap is moved relative to the scene.

jano_alex_es
23rd October 2009, 08:52
Well, I think I found the basis of the problem, but I don't know how I could solve it.

The item is the blue line, it's a QGraphicsPixmapItem.
http://img26.imageshack.us/img26/2328/72528796.jpg


This is the rotated item (45º). The angle is kept into its member variable m_iAngle.
http://img26.imageshack.us/img26/9596/10151997.jpg


But, when the next code is executed:


//Set the item to angle zero in order to write its true position to the Xml file. Later, rotate it again.

float xx= myItem->pixmap().width() / 2;
float yy = myItem->pixmap().height() / 2;
myItem->setTransform(QTransform().translate(-xx, -yy).rotate(0).translate(xx, yy));



xmlWriter.writeStartElement("x");
xmlWriter.writeCharacters(QString::number(myItem->scenePos().x()));
xmlWriter.writeEndElement();
xmlWriter.writeStartElement("y");
xmlWriter.writeCharacters(QString::number(myItem->scenePos().y()));
xmlWriter.writeEndElement();

myItem->setTransform(QTransform().translate(-xx, -yy).rotate(myItem->getAngle()).translate(xx, yy));


But my item now appears:
http://img26.imageshack.us/img26/5886/27428008.jpg

Maybe I should stop using its width and height... but it worked well so far :S

Do you know what should I use instead? or should I renounce to rotate from its center?