本页内容

Qt Quick 示例 - 定位器

本页面汇集了 QML 定位器示例。

按“列”、“行”、“网格”和“流”定位器布局排列的彩色矩形

“定位器”是一组与定位器相关的简短 QML 示例。每个示例都是一个简短的 QML 文件,重点展示特定类型或功能。如需了解更多信息,请访问《Qt Quick 中的重要概念——定位》。

运行示例

您可以通过以下方式运行示例:

过渡效果

“过渡”功能在显示或隐藏定位器中的项目时,会呈现动画过渡效果。它包含一个场景,其中放置了位于各种定位器中的项目:Column 、Row 、Grid 以及Flow 。每个定位器都具有被描述为“过渡”的动画。

move: Transition {
    NumberAnimation {
        properties: "x,y"
        easing.type: Easing.OutBounce
    }
}

“移动”过渡效果指定了当定位器内的项目因其他项目的出现或消失而被位移时,其动画效果。

add: Transition {
    NumberAnimation {
        properties: "x,y"
        easing.type: Easing.OutBounce
    }
}

“add”过渡指定了项目被添加到定位器时如何出现。

populate: Transition {
    NumberAnimation {
        properties: "x,y"
        from: 200
        duration: 100
        easing.type: Easing.OutBounce
    }
}

“填充”过渡指定了当父定位器首次创建时,其中的项目将如何出现。

附加属性

“附加属性”展示了如何使用“定位器”附加属性来确定项目在定位器中的位置。

Rectangle {
    id: green
    color: "#80c342"
    width: 100 * page.ratio
    height: 100 * page.ratio

    Text {
      anchors.left: parent.right
      anchors.leftMargin: 20
      anchors.verticalCenter: parent.verticalCenter
      text: qsTr("Index: %1%2%3").arg(parent.Positioner.index)
                .arg(parent.Positioner.isFirstItem ? qsTr(" (First)") : "")
                .arg(parent.Positioner.isLastItem ? qsTr(" (Last)") : "")
    }

    // When mouse is clicked, display the values of the positioner
    MouseArea {
        anchors.fill: parent
        onClicked: column.showInfo(green.Positioner)
    }
}

示例项目 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.