首页 > 解决方案 > 用于方向更改的 QML 信号完成

问题描述

我正在为 iPad 开发 ESRI AppStudio 应用程序(AppStudio 3.1,Qt 5.11),并且需要在方向更改时对 QML 控件进行一些调整大小。我发现这个页面似乎描述了执行此操作的官方方式:https ://wiki.qt.io/QML_orientation_observer

import QtQuick.Window 2.2
Rectangle {
    property bool isPortrait: Screen.primaryOrientation === Qt.PortraitOrientation || Screen.primaryOrientation === Qt.InvertedPortraitOrientation
    onIsPortraitChanged: console.log("isPortrait", isPortrait)
}

但是,我发现该页面上关于在完成高度和宽度更改后将触发绑定的声明是不正确的。我在实现时看到的是 onIsPortraitChanged 在方向改变时确实会触发,但它会在方向改变动画完成之前和调整应用程序的宽度之前触发。有没有办法在宽度完成更改后触发我的代码?

标签: qtqml

解决方案


这是我找到的一个解决方案,但它仅适用于应用程序全屏的设备,并且可能有更简洁的方法来执行此操作。

import QtQuick.Window 2.2
Window {
    id: app
    visible: true
    width: 640
    height: 480

    Rectangle {
        anchors.fill: parent
        onWidthChanged: {
            if(app.width === Screen.width || app.width === Screen.height) {
                //calculate new size
            }
        }
    }
}

推荐阅读