• 6238阅读
  • 1回复

[提问]限制QGraphicsItem的子项移动范围问题 [复制链接]

上一主题 下一主题
离线zealot0801
 
只看楼主 倒序阅读 楼主  发表于: 2011-08-17
关键词: QGraphicsItem
现在的需求是使用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根本进不去,奇怪,高手们帮我想想办法
离线zealot0801
只看该作者 1楼 发表于: 2011-08-17
又仔细看了看助手,发现需要设置 setFlags(ItemSendsGeometryChanges) 后才能接收ItemPositionChange 消息,问题解决了~
快速回复
限100 字节
 
上一个 下一个