程序代码:
#include "myitem.h"
#include<QtGui>
MyItem::MyItem()
{
setFlags(ItemIsFocusable|ItemIsMovable);
boundingRect();
rect.setRect(-18,-18,36,36);
}
QRectF MyItem::boundingRect() const
{
return QRectF(rect);
}
void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
prepareGeometryChange();
rect.moveCenter(event->pos()-offset);
update();
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(rect);
}
void MyItem::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_4 :
rotate(-30);
break;
case Qt::Key_6 :
rotate(30);
break;
case Qt::Key_Up:
scale(1.2,1.2);
break;
case Qt::Key_Down:
scale(1/1.2,1/1.2);
break;
}
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
setFocus();
offset=event->pos()-rect.center();
}
#include<QtGui>
MyItem::MyItem()
{
setFlags(ItemIsFocusable|ItemIsMovable);
boundingRect();
rect.setRect(-18,-18,36,36);
}
QRectF MyItem::boundingRect() const
{
return QRectF(rect);
}
void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
prepareGeometryChange();
rect.moveCenter(event->pos()-offset);
update();
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(rect);
}
void MyItem::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_4 :
rotate(-30);
break;
case Qt::Key_6 :
rotate(30);
break;
case Qt::Key_Up:
scale(1.2,1.2);
break;
case Qt::Key_Down:
scale(1/1.2,1/1.2);
break;
}
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
setFocus();
offset=event->pos()-rect.center();
}
正常item缩放旋转都是以图形项坐标原点变化的,为什么我这么一改就只能按item初始位置的坐标点变化了?
item.setPos(x,y); 就按(x,y)点旋转、缩放,如果没设置就是场景坐标的(0,0)点。我的boundingRect也跟着改变了位置,为什么不是按着图形项坐标原点变化?