【发布时间】:2016-04-09 05:14:46
【问题描述】:
我已将我的QGraphicsItem 的boundingRect() 设置为场景中的某个坐标。如何根据QGraphicsItem::mouseMoveEvent更改坐标?
下面是我写的代码。但是这段代码仅将我在boundingRect() 中绘制的形状的位置设置为boundingRect() 中的坐标。我想要做的是将整个QGraphicsItem 移动到一个设定的坐标。
void QGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseMoveEvent(event);
if (y()>=394 && y()<425) //if in the range between 425 and 394 I want it to move to 440.
{
setPos(440, y());
}
else if ... //Other ranges such as that in the first if statement
{
... //another y coordinate
}
else //If the item is not in any of the previous ranges it's y coordinate is set to 0
{
setPos(0,y()); //I had expected the item to go to y = 0 on the scene not the boundingRect()
}
}
场景是 880 x 860,boundingRect() 设置如下:
QRectF QGraphicsItem::boundingRect() const
{
return QRectF(780,425,60,30);
}
【问题讨论】:
标签: qt mouseevent qgraphicsitem qgraphicsscene