本页内容

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 、move 和remove 。

当 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 会接管添加到视图中的项目的几何管理。不支持在项目上使用锚点,且任何对 `width ` 或 `height ` 的赋值都会被视图覆盖。请注意,这仅适用于项目的根节点。为其子项指定宽度和高度,或使用锚点,则会按预期工作。

另请参阅 TabBar 、PageIndicator 、 Qt Quick Controls 中的 《自定义 SwipeView》、《导航控件》、《容器控件》和《焦点管理》。

属性文档

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)中引入。

另请参阅 horizontal 和vertical 。

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 attached]

此附加属性存储了SwipeView 中每个子项的索引。

它被附加到SwipeView 的每个子项上。

SwipeView.isCurrentItem : bool [read-only attached]

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

它被附加到 `SwipeView` 的每个子项上。

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

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

该属性附加于SwipeView 的每个子项上。

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

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

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

该属性附加于 `SwipeView` 的每个子项上。

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

SwipeView.view : SwipeView [read-only attached]

此附加属性保存了管理该子项的视图。

它被附加到SwipeView 的每个子项上。

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