现在的需求是使用QGraphicsItem绘制一个滚动条,问题是如果一个QGraphicsItem中嵌套一个子项QGraphicsItem,如何可以让子项在拖动时不超出父项的边界?我试了qt助手中的
//QVariant ChildItem::itemChange(GraphicsItemChange change, const QVariant &value)
//{
// if (change == ItemPositionChange && parentItem()) {
// // value is the new position.
// QPointF newPos = value.toPointF();
// QRectF rect = parentItem()->boundingRect();
// 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);
//}
但是拖动子项的时候第一个if根本进不去,奇怪,高手们帮我想想办法