PDA

View Full Version : GraphicsItemChange and QTransform



ct-xuyaojun
28th September 2009, 10:16
i want to limit QGraphicsItem object move only inside in QGraphicsScene's sceneRect, i rewrite QGraphicsItem::itemChange function below like Qt assistant shows,

QVariant Rectangle::itemChange( GraphicsItemChange change, const QVariant &value )
{
if ( change == ItemPositionChange && scene() )
{
QPointF newPos = value.toPointF();
QRectF rect = scene()->sceneRect();
QRectF sbRect = sceneBoundingRect();
if ( !rect.contains( newPos ) )
{
qreal x = qMin( rect.right() - boundingRect().width(), qMax( sbRect.left(), rect.left() ) );
newPos.setX( x - boundingRect().x() );

qreal y = qMin( rect.bottom() - boundingRect().height(), qMax( sbRect.top(), rect.top() ) );
newPos.setY( y - boundingRect().y() );

return newPos;
}
}
return QGraphicsItem::itemChange( change, value );
}

and it works right,but when i rotate the item,
QPointF center = mouse->boundingRect().center();
mouse->setTransform( QTransform().translate( center.x(), center.y() ).rotate( 90 ).translate( -center.x(), -center.y() ), true );

it doesn't work! what is the reason?