Results 1 to 3 of 3

Thread: ScenePos changes when rotating an item

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default ScenePos changes when rotating an item

    Hi,

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

    Qt Code:
    1. float xx= this->pixmap().width() / 2;
    2. float yy= this->pixmap().height() / 2;
    3. setTransform(QTransform().translate(xx,yy).rotate(m_iAngle).translate(-xx, -yy));
    To copy to clipboard, switch view to plain text mode 


    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 .

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: ScenePos changes when rotating an item

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: ScenePos changes when rotating an item

    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.



    This is the rotated item (45º). The angle is kept into its member variable m_iAngle.



    But, when the next code is executed:
    Qt Code:
    1. //Set the item to angle zero in order to write its true position to the Xml file. Later, rotate it again.
    2.  
    3. float xx= myItem->pixmap().width() / 2;
    4. float yy = myItem->pixmap().height() / 2;
    5. myItem->setTransform(QTransform().translate(-xx, -yy).rotate(0).translate(xx, yy));
    6.  
    7.  
    8.  
    9. xmlWriter.writeStartElement("x");
    10. xmlWriter.writeCharacters(QString::number(myItem->scenePos().x()));
    11. xmlWriter.writeEndElement();
    12. xmlWriter.writeStartElement("y");
    13. xmlWriter.writeCharacters(QString::number(myItem->scenePos().y()));
    14. xmlWriter.writeEndElement();
    15.  
    16. myItem->setTransform(QTransform().translate(-xx, -yy).rotate(myItem->getAngle()).translate(xx, yy));
    To copy to clipboard, switch view to plain text mode 


    But my item now appears:


    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?

Similar Threads

  1. Replies: 1
    Last Post: 25th February 2009, 01:34
  2. ListWidget positions item
    By talex in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2008, 16:16
  3. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 20:21
  4. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 08:37
  5. Replies: 1
    Last Post: 19th April 2007, 23:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.