Results 1 to 4 of 4

Thread: How to rotate items in a QGraphicsScene while keeping them upright

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default How to rotate items in a QGraphicsScene while keeping them upright

    Hello, I'd like to be able to rotate some horizontal lines about the midpoint of a QGraphicsScene, but keep the lines themselves horizontal. Is there a simple way to do this? My only idea is to create a QTransform object and call QTransform::rotate, then use QTransform::map to map one of every line's two coordinates, then translate each line individually using QGraphicsItem::translate. But I can't figure out how I'd actually get each line's initial coordinate (for putting into QTransform::map), or if there is a more elegant and efficient way of doing it. Speed is a bit of a concern because there will be about one or two hundred lines, and the rotation will be continuous.

    I'd appreciate any advice.

  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: How to rotate items in a QGraphicsScene while keeping them upright

    Could you provide a clearer explanation of what you want to achieve? As now it seems you want to rotate things without them rotating which doesn't make much sense.
    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
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to rotate items in a QGraphicsScene while keeping them upright

    If I understand you right, you simply want to rotate the line mid point:
    Qt Code:
    1. QLine line = ...;
    2. QTransform t;
    3. t.rotate(angle); // actually just figure it out yourself how to rotate around specified point
    4. const QPoint mid = (line.p1()+line.p2)/2;
    5. const QPoint rotated_mid = t.map(mid);
    6. line.translate( rotated_mid-mid );
    To copy to clipboard, switch view to plain text mode 
    You cannot rotate the start and end points separately, it will change the line direction (if angle != 0).

  4. The following user says thank you to stampede for this useful post:

    wayfaerer (5th February 2012)

  5. #4
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: How to rotate items in a QGraphicsScene while keeping them upright

    Thank you! That worked, although I actually wound up finding a simpler solution for my specific case. The lines are on top of QGraphicsItems that *do* rotate normally. So all I had to do was set each line's parent as its corresponding QGraphicsItem, then set the ItemIgnoresTransformations flag on the lines. I think I'm going to wind up using your approach elsewhere, though. Thanks again.

Similar Threads

  1. QGraphicsScene is SLOW with a lot of items !
    By pl01 in forum Qt Programming
    Replies: 11
    Last Post: 5th August 2011, 16:06
  2. How to add items in a QGraphicsScene?
    By schmimona in forum Qt Programming
    Replies: 2
    Last Post: 3rd August 2011, 08:53
  3. Rotate Item qgraphicsscene
    By pispipepe in forum Qt Programming
    Replies: 5
    Last Post: 28th July 2010, 20:05
  4. Animating many items in a QGraphicsScene
    By Luc4 in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2010, 06:55
  5. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03

Tags for this Thread

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.