当前位置:   article > 正文

Qt基于Qml堆栈(StackView)窗口使用示例_qml stack

qml stack

演示效果:

1.声明窗口栈

  1. //窗口栈
  2. StackView {
  3. id: stack
  4. initialItem: mainView
  5. anchors.fill: parent
  6. }

2.指定页面窗口栈

page1.stack = stack;//设置页面栈

3.入栈

stack.push(page1);//入栈

4.出栈

onClicked: stack.pop(); //出栈

完整QML源码:

main.qml

  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3. ApplicationWindow {
  4. id: frmWindow
  5. title: qsTr("Qt基于Qml堆栈(StackView)窗口使用示例")
  6. width: 400
  7. height: 300
  8. visible: true
  9. //窗口栈
  10. StackView {
  11. id: stack
  12. initialItem: mainView
  13. anchors.fill: parent
  14. }
  15. Page {
  16. id: mainView
  17. Label{
  18. text: qsTr("Home")
  19. height: 80
  20. width: 240
  21. anchors.centerIn: parent
  22. font.pixelSize: 20
  23. }
  24. Button {
  25. height: 32
  26. width: 120
  27. text: qsTr("Setting")
  28. anchors.right: parent.right
  29. anchors.bottom: parent.bottom
  30. onClicked: {
  31. page1.visible = true;
  32. page1.stack = stack;//设置页面栈
  33. stack.push(page1);//入栈
  34. }
  35. }
  36. }
  37. Page1 {
  38. id: page1
  39. visible: false
  40. }
  41. }

page1.qml

  1. import QtQuick 2.8
  2. import QtQuick.Controls 2.1
  3. Page {
  4. id: frmWindow
  5. title: qsTr("设置")
  6. visible: true
  7. property StackView stack: null
  8. Button {
  9. height: 32
  10. width: 120
  11. text: "Pop"
  12. anchors.left: parent.left
  13. anchors.top: parent.top
  14. onClicked: stack.pop(); //出栈
  15. }
  16. Label{
  17. id: label
  18. text: qsTr("Setting Page")
  19. height: 80
  20. width: 240
  21. anchors.centerIn: parent
  22. font.pixelSize: 20
  23. }
  24. Button {
  25. height: 32
  26. width: 120
  27. text: qsTr("MySet")
  28. anchors.right: parent.right
  29. anchors.bottom: parent.bottom
  30. onClicked: {
  31. page2.visible = true;
  32. page2.stack = stack;//设置窗口栈
  33. stack.push(page2);//入栈
  34. }
  35. }
  36. Page2 {
  37. id: page2
  38. visible: false
  39. }
  40. }

page2.qml

  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3. Page {
  4. id: frmWindow
  5. title: qsTr("MySet")
  6. visible: true
  7. property StackView stack: null
  8. Button {
  9. height: 32
  10. width: 120
  11. text: "Pop"
  12. anchors.left: parent.left
  13. anchors.top: parent.top
  14. onClicked: stack.pop();//出栈
  15. }
  16. Label{
  17. id: label
  18. text: qsTr("MySet")
  19. height: 80
  20. width: 240
  21. anchors.centerIn: parent
  22. font.pixelSize: 20
  23. }
  24. Button {
  25. height: 32
  26. width: 120
  27. text: qsTr("Account")
  28. anchors.right: parent.right
  29. anchors.bottom: parent.bottom
  30. onClicked: {
  31. page3.visible = true;
  32. page3.stack = stack;//设置窗口栈
  33. stack.push(page3);//入栈
  34. }
  35. }
  36. Page3 {
  37. id: page3
  38. visible: false
  39. stack: stack
  40. }
  41. }

page3.qml

  1. import QtQuick 2.8
  2. import QtQuick.Controls 2.1
  3. Page {
  4. id: frmWindow
  5. title: qsTr("帐号")
  6. visible: true
  7. property StackView stack: null
  8. Button {
  9. height: 32
  10. width: 120
  11. text: "Pop"
  12. anchors.left: parent.left
  13. anchors.top: parent.top
  14. onClicked: stack.pop();//出栈
  15. }
  16. Label{
  17. id: label
  18. text: qsTr("帐号")
  19. height: 80
  20. width: 240
  21. anchors.centerIn: parent
  22. font.pixelSize: 20
  23. }
  24. Label{
  25. anchors.top: label.bottom
  26. anchors.horizontalCenter: parent.horizontalCenter
  27. text: qsTr("帐号信息")
  28. height: 80
  29. width: 240
  30. font.pixelSize: 20
  31. }
  32. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/334702
推荐阅读
相关标签
  

闽ICP备14008679号