Results 1 to 5 of 5

Thread: Rotating canvas item on its own center

  1. #1
    Join Date
    Feb 2006
    Location
    Knoxville, TN
    Posts
    14
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Rotating canvas item on its own center

    Hi,
    I'm trying to rotate several canvas items around their own center. I read on this forum and in other places that I need to translate the painter, rotate it, paint on it, and then restore it to its former state. I've tried this (or so I think), but I must be missing something. The rotation doesn't work properly, and with the painter translation/rotation code added, even boxes with a rotation of 0 show up in the wrong place.
    I've attached two screenshots: The first of the boxes without any translation/rotation code used. And the second code shows what happens when I add the translate/rotate funtions. Notice that even the unrotated boxes are translated, but they should be in exactly the same place as in the first screenshot. (The images are the same scale). The rotated boxes are placed at the same coordinates as the unrotated ones, so they should overlap if the code worked properly. Rotation is 55 and 19 degrees.




    Here is my code from drawShape

    Qt Code:
    1. void MSPBox::drawShape(QPainter &painter)
    2. {
    3. painter.translate(x(), y());
    4. painter.rotate(rotate);
    5.  
    6. QCanvasRectangle::drawShape(painter);
    7. painter.drawText(rect(), AlignCenter, text());
    8.  
    9. painter.rotate(-rotate);
    10. painter.translate(-x(), -y());
    11.  
    12. cout << rotate << " is the angle" << endl;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any suggestions!
    Last edited by cbeall1; 5th March 2006 at 17:08.

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

    Default Re: Rotating canvas item on its own center

    You should rotate the painter to be exactly in the centre of your item.

    Qt Code:
    1. painter.save();
    2. painter.translate(x()+width()/2, y()+height()/2);
    3. painter.rotate(rotate);
    4. painter.drawRect(-width()/2, -height()/2, width(), height());
    5. painter.drawText(-width()/2, -height()/2, width(), height(), AlignCenter, text());
    6. painter.restore();
    To copy to clipboard, switch view to plain text mode 

    BTW. You can get some artifacts if you don't provide a correct (rotated) bounding box for your item.
    Last edited by wysota; 5th March 2006 at 17:26.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rotating canvas item on its own center

    The problem is that QRect holds, apart from dimension, also the position. So either you have to use only width and height, or add another two translations.

    Try this:
    Qt Code:
    1. void MSPBox::drawShape( QPainter&painter )
    2. {
    3. QWMatrix old( painter.worldMatrix() );
    4.  
    5. const int dx = x() + width() / 2;
    6. const int dy = y() + height() / 2;
    7.  
    8. painter.translate( dx, dy );
    9. painter.rotate( rotate );
    10. painter.translate( -dx, -dy );
    11.  
    12. QCanvasRectangle::drawShape( painter );
    13. painter.drawText( rect(), AlignCenter, text() );
    14.  
    15. painter.setWorldMatrix( old );
    16.  
    17. cout << rotate << " is the angle" << endl;
    18. }
    To copy to clipboard, switch view to plain text mode 

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

    cbeall1 (6th March 2006)

  5. #4
    Join Date
    Feb 2006
    Location
    Knoxville, TN
    Posts
    14
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Rotating canvas item on its own center

    Thanks for both replies. Jacek, your method worked

    Now I have another question about using the canvas from multiple threads. I have a multithreaded server that will be receiving coordinates from multiple clients and I want to visualize this in real-time (I will move the boxes, and change the rotation). What do I need to do to make this threadsafe? I know that the canvas itself is just a data structure, so is it ok to add things to it/move things from any thread (using a mutex lock, of course), and only the update has to be called from the main GUI thread? I can't find any detailed info on this issue in the documentation.
    Thanks!

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

    Default Re: Rotating canvas item on its own center

    Quote Originally Posted by cbeall1
    What do I need to do to make this threadsafe?
    Use custom events to send data to the main thread which will modify canvas structures. Don't attempt to modify them from another thread! No matter if you use mutexes or not. You'd have to lock all operations on the canvas, including all its events, so it's better to use custom events.

  7. The following user says thank you to wysota for this useful post:

    cbeall1 (6th March 2006)

Similar Threads

  1. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  2. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  3. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  4. Child windows in QCanvas as a canvas item
    By incubator in forum Qt Programming
    Replies: 8
    Last Post: 18th July 2006, 14:50
  5. Replies: 3
    Last Post: 17th July 2006, 17:57

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
  •  
Qt is a trademark of The Qt Company.