SwipeView QML Type

使用户可以通过横向滑动来浏览页面。更多

Import Statement: import QtQuick.Controls
Inherits:

Container

属性

  • horizontal : bool (since QtQuick.Controls 2.3 (Qt 5.10))
  • interactive : bool (since QtQuick.Controls 2.1 (Qt 5.8))
  • orientation : enumeration (since QtQuick.Controls 2.2 (Qt 5.9))
  • vertical : bool (since QtQuick.Controls 2.3 (Qt 5.10))

附属物业

详细说明

SwipeView 提供了一种基于轻扫的导航模型。

SwipeView 包含一组页面。一次只能看到一个页面。用户可以通过横向轻扫在页面之间进行导航。请注意,SwipeView 本身完全是非可视的。建议将其与PageIndicator 结合使用,以便为用户提供有多个页面的视觉线索。

SwipeView {
    id: view

    currentIndex: 1
    anchors.fill: parent

    Item {
        id: firstPage
    }
    Item {
        id: secondPage
    }
    Item {
        id: thirdPage
    }
}

PageIndicator {
    id: indicator

    count: view.count
    currentIndex: view.currentIndex

    anchors.bottom: view.bottom
    anchors.horizontalCenter: parent.horizontalCenter
}

如上图所示,SwipeView 通常由一组静态页面填充,这些页面被内联定义为视图的子页面。此外,还可以在运行时动态添加add,insert,moveremove 页面。

当 SwipeView 与另一个容器(如TabBar )配对时,有必要在每个控件的currentIndex 属性之间进行双向绑定。要做到这一点而不破坏绑定,应避免直接设置currentIndex ,而应使用setCurrentIndex() 等。更多信息请参见Managing the Current Index

要在currentIndex 发生变化时执行操作,请使用onCurrentIndexChanged 属性变化信号处理程序

onCurrentIndexChanged: {
    print("currentIndex changed to", currentIndex)
    // ...
}

通常情况下,我们不建议在 SwipeView 中添加过多页面。但是,当页面数量增加或单个页面相对复杂时,可能需要通过卸载用户无法直接访问的页面来释放资源。下面的示例介绍了如何使用Loader 来保持最多三个页面同时实例化。

SwipeView {
    Repeater {
        model: 6
        Loader {
            active: SwipeView.isCurrentItem || SwipeView.isNextItem || SwipeView.isPreviousItem
            sourceComponent: Text {
                text: index
                Component.onCompleted: console.log("created:", index)
                Component.onDestruction: console.log("destroyed:", index)
            }
        }
    }
}

注意: SwipeView 接管了添加到视图中的项目的几何管理。不支持在项目上使用锚点,任何widthheight 分配都将被视图覆盖。请注意,这仅适用于项的根部。指定宽度和高度,或对其子项使用锚点都可以正常工作。

另请参阅 TabBar,PageIndicator,Customizing SwipeView,Navigation Controls,Container ControlsFocus Management inQt Quick Controls

属性文档

horizontal : bool [read-only, since QtQuick.Controls 2.3 (Qt 5.10)]

该属性表示轻扫视图是否水平。

该属性在 QtQuick.Controls 2.3 (Qt 5.10) 中引入。

另请参阅 orientation


interactive : bool [since QtQuick.Controls 2.1 (Qt 5.8)]

该属性描述了用户是否可以与SwipeView 交互。用户无法轻扫不交互的视图。

默认值为true

该属性在 QtQuick.Controls 2.1 (Qt 5.8) 中引入。


orientation : enumeration [since QtQuick.Controls 2.2 (Qt 5.9)]

该属性保存方向。

可能的值:

常量说明
Qt.Horizontal水平(默认)
Qt.Vertical垂直

此属性在 QtQuick.Controls 2.2 (Qt 5.9) 中引入。

另请参阅 horizontalvertical


vertical : bool [read-only, since QtQuick.Controls 2.3 (Qt 5.10)]

该属性用于确定轻扫视图是否垂直。

该属性在 QtQuick.Controls 2.3 (Qt 5.10) 中引入。

另请参阅 orientation


附加属性文档

SwipeView.index : int [read-only]

该附加属性保存SwipeView 中每个子项目的索引。

它附加到SwipeView 的每个子项。


SwipeView.isCurrentItem : bool [read-only]

如果该子项目是当前项目,则该附加属性为true

它附加到SwipeView 的每个子项。


SwipeView.isNextItem : bool [read-only, since QtQuick.Controls 2.1 (Qt 5.8)]

如果该子项目是下一个项目,则该附加属性为true

它附加到SwipeView 的每个子项。

此属性在 QtQuick.Controls 2.1 (Qt 5.8) 中引入。


SwipeView.isPreviousItem : bool [read-only, since QtQuick.Controls 2.1 (Qt 5.8)]

如果该子项目是上一个项目,则该附加属性为true

它附加到SwipeView 的每个子项目。

该属性在 QtQuick.Controls 2.1 (Qt 5.8) 中引入。


SwipeView.view : SwipeView [read-only]

该附加属性包含管理该子项目的视图。

它附加到SwipeView 的每个子项目。


© 2025 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.