void Scene::restoreAllItemsPosition()
{
for(int i = 0; i < total_stu_list.length(); i++)
{
Item *item = total_stu_list.at(i);
QPointF pos = item->scenePos();
QRectF boundsRect = sceneRect();
if (pos.rx() <= (boundsRect.x()+10) || pos.ry() <= (boundsRect.y()+10)
|| pos.rx() >= (boundsRect.width()-50)||pos.ry() >= (boundsRect.height()- 50))
{
item->setPos(item->getItemPos());
}
item->setItemPos(item->scenePos());
}
}
代码片段如上,如果我判断了item拖动到了场景不可见处,调用此代码,将其恢复到上次的场景位置中,该函数在Scene的mouseMove函数中调用。