Qt qml StackView and GridView and Loader

Qt qml StackView and GridView

main: loader component
//main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.0

Window {
    id: root
    width: 800
    height: 480
    visible: true
    title: qsTr("Hello World")

    Rectangle{
        id: mainBackGround
        anchors.centerIn: parent
        width: 580
        height: 360
        color: "lightGray"
        clip: true

        Loader{
            id: mainLoader
            anchors.leftMargin: 20
            anchors.topMargin: 20
            anchors.top: mainBackGround.top
            anchors.left: mainBackGround.left
            anchors.right: mainBackGround.right
            anchors.bottom: mainBackGround.bottom
            source: "StackViewPage.qml"
        }
    }
}

stack view component: StackViewPage.qml
//StackViewPage.qml
import QtQuick 2.0
import QtQuick.Controls 2.12

Item {
    anchors.fill: parent
    StackView{
        id: stackView
        anchors.fill: parent
        initialItem: "GridOne.qml"
    }
}
stack view Page one : GridOne.qml
//GridOne.qml
import QtQuick 2.0
import QtQml.Models 2.15
import QtQuick.Controls 2.12

Item {
    ListModel{
        id: listModel
        ListElement { name: "One"; page: "StationPage.qml"}
        ListElement { name: "Two"; page: ""}
        ListElement { name: "Three"; page: ""}
        ListElement { name: "Four"; page: ""}
        ListElement { name: "Five"; page: ""}
        ListElement { name: "Six"; page: ""}
        ListElement { name: "Seven"; page: ""}
        ListElement { name: "Eight"; page: ""}
    }

    GridView{
        id: gridView
        anchors.fill: parent
        model: listModel
        cellHeight: 140
        cellWidth: 140
        delegate: Rectangle{
            width: 120
            height: 120
            color: "grey"
            radius: 5
            Text{
                color: "white"
                text: name
                anchors.centerIn: parent
            }
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    if(index === 0){
                        mainLoader.source = page
                    }
                }
            }
        }
    }

    Button{
        id: nextButton
        anchors{
            right: parent.right
            bottom: parent.bottom
            margins: 20
        }

        text: "Next"
        onClicked: stackView.push("GridTwo.qml")
    }

    Button{
        id: previousButton
        anchors{
            right: nextButton.left
            margins: 10
            verticalCenter: nextButton.verticalCenter
        }

        text: "Previous"
    }
}

stack view Page two: GridTwo.qml
//GridTwo.qml
import QtQuick 2.0
import QtQml.Models 2.15
import QtQuick.Controls 2.12

Item {
    ListModel{
        id: listModel
        ListElement { name: "001"; page: ""}
        ListElement { name: "002"; page: ""}
        ListElement { name: "003"; page: ""}
        ListElement { name: "004"; page: ""}
        ListElement { name: "005"; page: ""}
        ListElement { name: "006"; page: ""}
        ListElement { name: "007"; page: ""}
        ListElement { name: "008"; page: ""}
    }

    GridView{
        id: gridView
        anchors.fill: parent
        model: listModel
        cellHeight: 140
        cellWidth: 140
        delegate: Rectangle{
            width: 120
            height: 120
            color: "grey"
            radius: 5
            Text{
                color: "white"
                text: name
                anchors.centerIn: parent
            }
        }
    }

    Button{
        id: nextButton
        anchors{
            right: parent.right
            bottom: parent.bottom
            margins: 20
        }

        text: "Next"
    }

    Button{
        id: previousButton
        anchors{
            right: nextButton.left
            margins: 10
            verticalCenter: nextButton.verticalCenter
        }

        text: "Previous"
        onClicked: stackView.push("GridOne.qml")
    }
}

supplement
//StationPage.qml
import QtQuick 2.0

Item {
    anchors.fill: parent
    Rectangle{
        anchors.fill: parent
        color: "blue"
        Text{
            text: "Test"
            font.pixelSize: 48
            font.bold: true
            anchors.centerIn: parent
            color: "lightgray"
        }

        MouseArea{
            anchors.fill: parent
            onClicked: mainLoader.source = "StackViewPage.qml"
        }
    }
}

效果

stackViewAndGridViewAndLoader

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值