• 3027阅读
  • 2回复

[提问]QGraphicsView拖拽图片时如何不露出白色的底 [复制链接]

上一主题 下一主题
离线gw4824521
 

只看楼主 倒序阅读 楼主  发表于: 2016-12-28
QGraphicsView如何实现拖拽图像到达图像边缘的时候无法再拖拽,不会露出白边如下图:


而不是像下图这样:



不知道QGraphicsView有没有直接设置的方法
离线firebolt

只看该作者 1楼 发表于: 2016-12-29
QVariant GraphicsItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if ( change == QGraphicsItem::ItemSelectedHasChanged ) {
        QGraphicsItemGroup *g = dynamic_cast<QGraphicsItemGroup*>(parentItem());
        if (!g)
            setState(value.toBool() ? SelectionHandleActive : SelectionHandleOff);
        else{
            setSelected(false);
            return QVariant::fromValue<bool>(false);
        }
    }

    else 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()-boundingRect().width()/2, qMax(newPos.x(), rect.left()+boundingRect().width()/2)));
            newPos.setY(qMin(rect.bottom()-boundingRect().height()/2, qMax(newPos.y(), rect.top()+boundingRect().height()/2)));
            return newPos;
        }
    }

    return QGraphicsItem::itemChange(change, value);
}
离线kongxz

只看该作者 2楼 发表于: 2017-01-10
超过范围后,改变鼠标状态变成释放,把图片移到边缘位置
快速回复
限100 字节
 
上一个 下一个