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.
@
MyClass::MyClass(QWidget *parent) : QGraphicsView(parent)
{
scene = new QGraphicsScene( this );
this->setScene(scene);QPixmap px( "001.jpg" ); pItem = scene->addPixmap( px ); pItem->setScale( 0.1 ); pItem->setTransformationMode( Qt::SmoothTransformation );
}
void MyClass::Rotate( qreal angle )
{
qreal old_angle = m_currentAngle; // m_currentAngle is a member var, with the actual angle
m_currentAngle = angle;
transform.rotate( m_currentAngle - old_angle, Qt::XAxis );
pixmapItem->setTransform( transform );
}
@The rotation occurs, but we can see the wrong "anchor point"...
-
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
@pixmapItem->setTransformOriginPoint(pixmapItem->boundingRect().center());@ -
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:
@
transform.translate( x, y );
transform.rotate( angle - old_angle );
transform.translate( -x, -y );
scene->items().at(0)->setTransform( transform );
@Thank you again.
-
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. :)