我有一个QGraphicsView
和一个QGraphicsScene
。根据用户输入,一些QGraphicsItem
可能会被放置在场景中。该项目既可选择又可移动。
当场景大于视图滚动条时出现(它们被设置为在必要时显示)。
当用户在视图边缘附近移动项目时,场景宽度/高度会相应拉伸 - 我正在使场景更大。
问题是当项目靠近视图边界时,如何强制滚动条随场景滚动?我认为在任何图形编辑器中都很常见的功能。在MouseMoveEvent
场景中,我将场景放大,强制滑块移动并相应地更新可见矩形。
这不能按预期工作。即使卷轴正在调整到新的场景大小,视图中也没有平滑的移动。有没有更好的方法来做到这一点?
一些解释:
itemUnderCursor = currently slected QGraphicsItem
qgv = QGraphicsView
代码片段:
// check if item is near the border
QPointF point = itemUnderCursor->mapToScene(itemUnderCursor->boundingRect().topLeft());
double delta = 0;
if(point.x() < visibleRect.left())
{
// prevent from drawing outside the scene
itemUnderCursor->setPos(visibleRect.left(), itemUnderCursor->scenePos().y());
if(event->scenePos().x() < oldMousePos.x()-3)
{
// stretch the scene
if(qgv->horizontalScrollBar()->value() <= 0)
setSceneRect(QRectF(QPointF(sceneRect().x() - 3, sceneRect().y()), sceneRect().bottomRight()));
/*
* disable signals from DrawingArea in order to avoid
* recursive calls of mouseMoveEvent then enabling them
* back to handle the rest of events
*/
this->blockSignals(true);
delta = point.x() - originalRect.left();
qgv->horizontalScrollBar()->setValue(hScrollOriginalValue + delta);
}
oldMousePos = event->scenePos();
this->blockSignals(false);
// update the visible rectangle
visibleRect = getVisibleRect(qgv);
}
if(point.x() + itemUnderCursor->boundingRect().width() > visibleRect.right())
{
// prevent from drawing outside the scene
itemUnderCursor->setPos(visibleRect.right() - itemUnderCursor->boundingRect().width(), itemUnderCursor->scenePos().y());
if(event->scenePos().x() > oldMousePos.x()+3)
{
// stretch the scene
if(qgv->horizontalScrollBar()->value() >= 0)
setSceneRect(QRectF(sceneRect().topLeft(), QPointF(sceneRect().bottomRight().x() + 3, sceneRect().bottomRight().y())));
/*
* disable signals from DrawingArea in order to avoid
* recursive calls of mouseMoveEvent then enabling them
* back to handle the rest of events
*/
delta = point.x() + itemUnderCursor->boundingRect().width() - originalRect.right();
this->blockSignals(true);
qgv->horizontalScrollBar()->setValue(hScrollOriginalValue + delta);
}
oldMousePos = event->scenePos();
this->blockSignals(false);
// update the visible rectangle
visibleRect = getVisibleRect(qgv);
}
我对QGraphicsView
.