How would I take the topleft corner as seen through the QGraphicsView and keep it the topleft while scaling?
So if I happen to have the symbol 'A' in the topleft corner while scaling, the A will stay there yet scales. At present the center of the screen is taking as the scale origin. But I would like the topleft corner be the origin for transformation.
That is, topleft as seen in the graphics 开发者_开发知识库view, not of the total graphics scene. How would I do this?
This is my scale code to scale the scene at 100% of it's width to the viewport:
void GraphicsView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
double scale_delta = (double) event->size().width() / scene()->width();
resetMatrix();
scale(scale_delta, scale_delta);
}
SOLUTION
void GraphicsView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
if (!first_shown)
{
centerOn(0, 0);
first_shown = true;
}
QPointF topleft = mapToScene(viewport()->rect().topLeft());
resetMatrix();
QPointF shift = (mapToScene(viewport()->rect().bottomRight() + QPoint(1, 1)) - mapToScene(viewport()->rect().topLeft()));
shift /= (double) event->size().width() / scene()->width();
fitInView(QRectF(topleft, topleft + shift));
}
If I understand your question correctly, the basic steps you'll need are:
- Translate Qt's idea of the origin to where you want to scale around, i.e. top-left
- Then apply the scale
- Then translate back again
精彩评论