查看完整版本: [-- QGraphicsView拖拽图片时如何不露出白色的底 --]

QTCN开发网 -> Qt基础编程 -> QGraphicsView拖拽图片时如何不露出白色的底 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

gw4824521 2016-12-28 16:31

QGraphicsView拖拽图片时如何不露出白色的底

QGraphicsView如何实现拖拽图像到达图像边缘的时候无法再拖拽,不会露出白边如下图:
[attachment=16283]

而不是像下图这样:

[attachment=16284]

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

firebolt 2016-12-29 10:11
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 2017-01-10 14:00
超过范围后,改变鼠标状态变成释放,把图片移到边缘位置


查看完整版本: [-- QGraphicsView拖拽图片时如何不露出白色的底 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled