我有一个QGraphicsView
小部件,场景中有很多物品。我在 ctr + 鼠标左键单击上平移视图并将其缩放到使用鼠标左键拖动创建的橡皮筋矩形。我看不到橡皮筋选择矩形(虚线),而橡皮筋选择功能工作正常。任何人都可以帮助我理解这一点吗?
在我看来,我使用这些标志:
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
setRenderHints(QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
setOptimizationFlag(QGraphicsView::DontSavePainterState,true);
setCacheMode(QGraphicsView::CacheBackground);
setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
setViewport(new QGLWidget);
下面是我的事件处理程序。
鼠标按下事件处理程序:
void MyView::mousePressEvent(QMouseEvent* event)
{
if(event->button()==Qt::LeftButton)
{
if(event->modifiers()==Qt::ControlModifier)
{
setDragMode(QGraphicsView::NoDrag);
m_rubberBandActive = false;
mousepressed=true;
m_lastDragPos = event->pos();
return;
}
else
{
setDragMode(QGraphicsView::RubberBandDrag);
m_rubberBandOrigin = event->pos();
m_rubberBandActive = true;
}
}
event->accept();
}
else
{
QWidget::mousePressEvent(event);
}
}
鼠标移动事件:
void MyView::mouseMoveEvent(QMouseEvent* event)
{
if(mousepressed)
{
QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
this->panView(delta);
m_lastDragPos = event->pos();
return;
}
event->accept();
}
鼠标释放事件处理程序:
void MyView::mouseReleaseEvent(QMouseEvent *event)
{
if (m_rubberBandActive)
{
QPoint rubberBandEnd = event->pos();
QRectF zoomRectInScene = QRectF(mapToScene(m_rubberBandOrigin),mapToScene(rubberBandEnd));
fitInView(zoomRectInScene, Qt::KeepAspectRatio);
m_rubberBandActive = false;
}
mousepressed=false;
event->accept();
}
平移视图:
void MyView::panView(QPointF delta)
{
QPoint viewCenter(viewport()->width() / 2 + delta.x(), viewport()->height() / 2 + delta.y());
QPointF newCenter = mapToScene(viewCenter);
centerOn(newCenter);
}