首页| 论坛| 消息

标题:QGraphicsView拖拽图片时如何不露出白色的底
作者:gw4824521
日期:2016-12-28 16:31
内容:

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

而不是像下图这样:

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


#1 [firebolt 12-29 10:11]
QVariant GraphicsItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if ( change == QGraphicsItem::ItemSelectedHasChanged ) {
QGraphicsItemGroup *g = dynamic_cast(parentItem());
if (!g)
setState(value.toBool() ? SelectionHandleActive : SelectionHandleOff);
else{
setSelected(false);
return QVariant::fromValue(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);
}
#2 [kongxz 01-10 14:00]
超过范围后,改变鼠标状态变成释放,把图片移到边缘位置

回复 发表
主题 版块