Important: Please read the Qt Code of Conduct -

QQuickView custom resize to behave in a PreserveAspectRatio - mode

  • I am wondering if there is a simple way of implementing (or maybe there is already there, and only waits to be used) a resize mechanism which behaves like PreserveAspectRatio does for Image qml-component.
    In my application the entire UI is developed using QtQuick, and the main qml will be loaded inside one QQuickView.
    UI is implemented in a static way. Every component and item has a fix position, and images which are used are also fixed.

    I know that as a best practice, I should use anchors in qml, and I should create the entire UI like in web technologies (everything is relative to parent's properties). Than when the resize mode is set to SizeRootObjectToView, it will work.

    But changing my entire UI will be time consuming, and I am thinking to leave it like it is now, and only the find a strategy to resize QQuickView in a PreserveAspectRatio way.

    Any sugestions from where I should start?

  • the easiest way is to react on resizeEvent, when the main window is resized, I just keep the new size and emit a signal scaledsizeChanged which later will be used inside the myitem.

    @Q_PROPERTY(QRectF scaledsize READ scaledsize WRITE setScaledsize NOTIFY scaledsizeChanged)
    void QtQuick2ApplicationViewer::resizeEvent(QResizeEvent *e)

    emit scaledsizeChanged();

    in main qml I will react on this signal, and call the resize method from myitem.

    @import QtQuick 2.0
    import com.mycompany.qmlcomponents 1.0

    MyItem {

    Connections {
        target: ApplicationViewer
        onScaledsizeChanged: resizePreserveAspectRatio(ApplicationViewer.scaledsize)
    //initial size
    width: 640;  height: 480@

    @void MyItem::resizePreserveAspectRatio(const QRectF &r)

    In this way QuickView will scale accordingly with the space available in the main Window.

    but still, is there a way of doing this, using an existing API?

Log in to reply