首先自己subclass QGraphicsView的一个类,叫DiagramView,然后重新实现它的滚轮事件函数,然后发送一个缩放信号:
oid DiagramView::wheelEvent(QWheelEvent * event){ if (event->delta() > )
{
emit mouseWheelZoom(true);
}
else
{
emit mouseWheelZoom(false);
} }
然后用connect把这个信号连接到要实现的槽函数上(ScaleFactor初始化为30):
void TopologyEditor::Zoom(bool zoom){ if (zoom && scaleFactor >= )
{ scaleFactor += ;
QMatrix old_matrix;
old_matrix = view->matrix();
view->resetMatrix();
view->translate(old_matrix.dx(), old_matrix.dy());
view->scale(scaleFactor/100.0, scaleFactor/100.0);
}
else if (!zoom && scaleFactor >= )
{
scaleFactor -= ;
QMatrix old_matrix;
old_matrix = view->matrix();
view->resetMatrix();
view->translate(old_matrix.dx(), old_matrix.dy()); view->scale( scaleFactor/100.0, scaleFactor/100.0); } else if (scaleFactor < ){ scaleFactor = 0.0;
} }
references:
http://*.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
http://www.qtcentre.org/threads/52603-Zoom-effect-by-mouse-Wheel-in-QGraphicsview