简体   繁体   English

如何在QQuickView中缩放整个QML2场景?

[英]How to zoom a whole QML2 scene in QQuickView?

I have a QML2 document which is significantly larger than the display on which it's displayed with a QQuickView. 我有一个QML2文档,该文档明显大于使用QQuickView显示的文档。 In QML1 and QtDeclarative it was possible to use QGraphicsView::fitInView to scale the whole scene (including correct MouseEvent mapping, etc). 在QML1和QtDeclarative中,可以使用QGraphicsView::fitInView缩放整个场景(包括正确的MouseEvent映射等)。

Is there something similar for QML2 I just didn't find yet? 我刚刚没有找到的QML2是否有类似的东西?

I came up with a solution that somehow behaves like QGraphicsView::fitInView(aRect, Qt::KeepAspectRatio) . 我想出了一个解决方案,其行为类似于QGraphicsView::fitInView(aRect, Qt::KeepAspectRatio)

void MyQuickView::fitInView(const QRectF & newRect)
{
    QSizeF newSize = newRect.size();
    qreal horizontalScale = size().width() / newSize.width();
    qreal verticalScale = size().height() / newSize.height();

    // You might want to use another origin
    rootObject()->setTransformOrigin(QQuickItem::TopLeft);

    rootObject()->setSize(newSize);
    rootObject()->setScale(qMin(horizontalScale, verticalScale));
}

This does mostly the job, although I haven't thoroughly tested all of my old Qt4.8 code. 尽管我还没有对所有旧的Qt4.8代码进行彻底的测试,但这基本上可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM