qml StackView
时间: 2024-02-01 18:03:04 浏览: 179
以下是两个关于QML StackView的示例:
1. 使用replace()方法进行页面切换
```qml
import QtQuick
import QtQuick.Controls
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
StackView {
id: stackView
anchors.fill: parent
initialItem: Component {
id: page
Page {
Row {
spacing: 20
anchors.centerIn: parent
Button {
text: "<"
onClicked: stackView.replace(page, StackView.PopTransition)
}
Button {
text: ">"
onClicked: stackView.replace(page, StackView.PushTransition)
}
}
}
}
}
}
```
2. 使用push()和pop()方法进行页面切换
```qml
import QtQuick
import QtQuick.Controls
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
StackView {
id: stack
initialItem: mainView
anchors.fill: parent
}
Component {
id: mainView
Row {
spacing: 10
Button {
text: "Push a page"
onClicked: stack.push(mainView)
}
Button {
text: "Pop a page"
enabled: stack.depth > 1
onClicked: stack.pop()
}
Text {
text: "Current stack depth: " + stack.depth
}
}
}
}
```
阅读全文