March 16, 2012

msx_br msx_br
Lab Rat
36 posts

Finding the center of QGraphicsPixmapItem

 

Hello,

I need to apply transformations in one QGraphicsPixmapItem, but i can’t find a way to
do this correctly.

I don’t know how to determine the center of the QGraphicsPixmapItem to apply setTransformOriginPoint and then setTransform.

  1. MyClass::MyClass(QWidget *parent) : QGraphicsView(parent)
  2. {
  3.     scene = new QGraphicsScene( this );
  4.     this->setScene(scene);
  5.  
  6.     QPixmap px( "001.jpg" );
  7.     pItem =  scene->addPixmap( px );
  8.     pItem->setScale( 0.1 );
  9.     pItem->setTransformationMode( Qt::SmoothTransformation );
  10. }
  11.  
  12. void MyClass::Rotate( qreal angle )
  13. {
  14.     qreal old_angle = m_currentAngle;    // m_currentAngle is a member var, with the actual angle
  15.     m_currentAngle  = angle;
  16.     transform.rotate( m_currentAngle - old_angle, Qt::XAxis );
  17.     pixmapItem->setTransform( transform );    
  18. }

The rotation occurs, but we can see the wrong “anchor point”…

 Signature 

msx_br - Brazil

3 replies

March 16, 2012

ludde ludde
Lab Rat
287 posts

Have you tried using QGraphicsItem::rotate() and/or QGraphicsItem::setTransformOriginPoint()?
To rotate around the center of the bounding rect, I would assume you should do something like

  1. pixmapItem->setTransformOriginPoint(pixmapItem->boundingRect().center());

March 16, 2012

msx_br msx_br
Lab Rat
36 posts

Thank you ludde,

I tried you suggestion, but the rotation has the originPoint centered at the topLeft corner.
( boundingRect().center() have no effect ) maybe we found a bug?

What resolved the case:

  1.     transform.translate( x, y );
  2.     transform.rotate( angle - old_angle );
  3.     transform.translate( -x, -y );
  4.     scene->items().at(0)->setTransform( transform );

Thank you again.

 Signature 

msx_br - Brazil

March 19, 2012

ludde ludde
Lab Rat
287 posts

OK, I think this is because you are creating a transformation instead of using the QGraphicsItem::rotate() function. If you create and set a transformation you probably have to do the translation (moving to the center and back) yourself. So probably not a bug, but could be better documented.
Anyway, good that you found a way that works, even if it was not exactly what I suggested. :)

 
  ‹‹ [Close This] Shortcut Ctrl+C in a table behaves differently in Mac OSX than in Linux, Windows      [SOLVED] making Qpainter graphics clickable ››

You must log in to post a reply. Not a member yet? Register here!