Results 1 to 5 of 5

Thread: Rotating pixmap

  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 Rotating pixmap

    Hello, I want to rotate the pixmap of the QGraphicsPixmap item and show the item rotated. Rotating the item cause other sort of problems.

    So far, I never had problems when rotating the pixmaps, but I don't know why now the rotation is not taking from the center and rotates the pixmap, it's like its rotated without the translation and I don't know why.

    Qt Code:
    1. //m_pxOriginal is a copy of the original pixmap. Not rotated.
    2. qreal rRadius = m_pxOriginal.width() / 2;
    3.  
    4. //Rotate the previous rotation - 10 degrees
    5. m_iAngle -= 10;
    6.  
    7. QPixmap rotatedPix = m_pxOriginal .transformed(QTransform().translate(-rRadius, -rRadius).rotate(m_iAngle).translate(rRadius, rRadius));
    8.  
    9. m_pxItem->setPixmap(rotatedPix);
    To copy to clipboard, switch view to plain text mode 

    Anything strange? :S thanks!

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Rotating pixmap

    Step one: get rid of the multiple transformations all packed onto a single line. Do them one at a time, examine each step and isolate the problem.

  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: Rotating pixmap

    The point is the rotation of the pixmaps. I think there is something wrong, because the rotation of the item works fine (unfortunatly, I do need to create the rotation feeling through the pixmap rotation, item rotation is not a possibility). Maybe there is a problem when transforming from/to QImage.


    Item rotation

    Qt Code:
    1. m_pxItem->setTransformOriginPoint(rRadius, rRadius);
    2. m_pxItem->setRotation(m_iAngle);
    To copy to clipboard, switch view to plain text mode 




    Pixmap rotation

    Qt Code:
    1. QPixmap pix1 = m_pxOriginal.transformed(QTransform().translate(-rRadius, -rRadius));
    2. QPixmap pix2 = m_pxOriginal.transformed(QTransform().rotate(m_iAn gle));
    3. QPixmap rotatedPix = pix2.transformed(QTransform().translate(rRadius, rRadius));
    4. m_pxItem->setPixmap(rotatedPix);
    5. //Just rotating, without the traslation, shows the same bounding effect
    To copy to clipboard, switch view to plain text mode 

    Last edited by jano_alex_es; 18th December 2010 at 11:12.

  4. #4
    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: Rotating pixmap

    I was able to workaround the problem thanks drawing in a rotated QPainter.

    Qt Code:
    1. QPixmap rotatedPixmap(m_pxOriginal.size());
    2. QPainter* p = new QPainter(&rotatedPixmap);
    3. QSize size = m_pxOriginal.size();
    4. p->translate(size.height()/2,size.height()/2);
    5. p->rotate(m_iAngle);
    6. p->translate(-size.height()/2,-size.height()/2);
    7. p->drawPixmap(0, 0, m_pxOriginal);
    8. p->end();
    9. delete p;
    10. m_pxItem->setPixmap(rotatedPixmap);
    To copy to clipboard, switch view to plain text mode 

    The rotation works fine! BUT a grey rectangle is painted:




    Why is it created? and more important, how can I remove it?

  5. #5
    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: Rotating pixmap

    Well, finally solved with the QPainter aproximation.

    Qt Code:
    1. //Please note this method takes 2 mseg to finish for a 20x20 pixmap.
    2. QPixmap rotatedPixmap(m_pixOriginal.size());
    3. rotatedPixmap.fill(QColor::fromRgb(0, 0, 0, 0)); //the new pixmap must be transparent.
    4. QPainter* p = new QPainter(&rotatedPixmap);
    5. QSize size = m_pxOriginal.size();
    6. p->translate(size.height()/2,size.height()/2);
    7. p->rotate(m_iAngle);
    8. p->translate(-size.height()/2,-size.height()/2);
    9. p->drawPixmap(0, 0, m_pxOriginal);
    10. p->end();
    11. delete p;
    12. m_pxItem->setPixmap(rotatedPixmap);
    To copy to clipboard, switch view to plain text mode 


    Definitly there is a translation problem when rotating a QPixmap, I don't know if its because the conversion to/from QImage or the known small rotation (according the QtAssistan it's possible to solve it with QPixmap::trueMatrix, but It doesn't solve it in my code) but the pixmap rotation is an issue in Qt.
    Last edited by jano_alex_es; 19th December 2010 at 04:22.

Similar Threads

  1. Rotating Gradient
    By JeffC in forum Newbie
    Replies: 3
    Last Post: 3rd June 2012, 11:11
  2. rotating a widget
    By oguzy in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2008, 00:08
  3. Rotating the polygon
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 4th September 2008, 00:41
  4. Rotating a slider...
    By ACFred in forum Newbie
    Replies: 7
    Last Post: 24th February 2008, 15:56
  5. Rotating QGraphicsItem
    By Gopala Krishna in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2006, 11:50

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.