SwipeView QML Type
使用户可以通过横向滑动来浏览页面。更多
Import Statement: | import QtQuick.Controls |
Inherits: |
属性
- 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))
附属物业
- index : int
- isCurrentItem : bool
- isNextItem : bool
(since QtQuick.Controls 2.1 (Qt 5.8))
- isPreviousItem : bool
(since QtQuick.Controls 2.1 (Qt 5.8))
- view : SwipeView
详细说明
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,Customizing SwipeView,Navigation Controls,Container Controls 和Focus Management inQt Quick Controls 。
属性文档
horizontal : bool |
interactive : bool |
orientation : enumeration |
该属性保存方向。
可能的值:
常量 | 说明 |
---|---|
Qt.Horizontal | 水平(默认) |
Qt.Vertical | 垂直 |
此属性在 QtQuick.Controls 2.2 (Qt 5.9) 中引入。
另请参阅 horizontal 和vertical 。
vertical : bool |
附加属性文档
SwipeView.isNextItem : bool |
SwipeView.isPreviousItem : bool |
© 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.