我认为我的问题与此类似post https://stackoverflow.com/questions/3548254/restrict-movable-area-of-qgraphicsitem但在 C++ 和 QGraphicsItem 中。
我想修复另一个 QGraphicsItem 中对象的可移动区域。如果我尝试将对象移到外面,我希望它留在里面。
也许这个想法是使用setParentItem()
.
有人知道如何限制 QGraphicsItem 内的可移动区域吗?
是的,你是对的。如在here https://stackoverflow.com/questions/3548254/restrict-movable-area-of-qgraphicsitem你必须重新实现 itemChange。来自qt文档
QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPos = value.toPointF();
QRectF rect = scene()->sceneRect();
if (!rect.contains(newPos)) {
// Keep the item inside the scene rect.
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
return newPos;
}
}
return QGraphicsItem::itemChange(change, value);
}
其中 scene() 指的是项目所在的 QGraphicsScene。如果您不使用 QGraphicScene,则必须适当地设置 QRectF(可能来自父项几何图形)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)