QML:ApplicationWindow中的menuBar、header、footer

一、ApplicationWindow简介

ApplicationWindow是一个窗口,可以方便地在窗口中添加菜单栏、页眉和页脚项。
您可以将ApplicationWindow声明为应用程序的根项,并使用QQmlApplicationEngine运行它。通过这种方式,您可以从QML控制窗口的属性、外观和布局。

二、Menu bar 代码示例

menuBar: MenuBar
    {
        Material.background: "#a8c0fd"

        Menu
        {
            title: "主食"
            MenuItem { text: "米饭" }
            MenuItem { text: "面条" }
            MenuItem { text: "米线" }
        }

        Menu
        {
            title: "蔬菜"
            MenuItem { text: "香葱" }
            MenuItem { text: "白菜" }
            MenuItem { text: "冬瓜" }
            MenuItem { text: "南瓜" }
        }

        Menu
        {
            title: "水果"
            MenuItem { text: "苹果" }
            MenuItem { text: "香蕉" }
            MenuItem { text: "哈密瓜" }
            MenuItem { text: "葡萄" }
            MenuItem { text: "草莓" }

            MenuSeparator{} //分割线

            MenuItem { text: "西瓜" }
            MenuItem { text: "樱桃" }
            MenuItem { text: "猕猴桃" }

            MenuSeparator{}

            MenuItem { text: "山竹" }
            MenuItem { text: "莲雾" }
            MenuItem { text: "小番茄" }
        }

        Menu
        {
            title: "饮品"

            Menu
            {
                title: "酒类"
                MenuItem { text: "白酒    ¥15" }
                MenuItem { text: "啤酒    ¥3" }
                MenuItem { text: "鸡尾酒   ¥9" }
            }

            Menu
            {
                title: "果汁"
                MenuItem { text: "西瓜汁   ¥2" }
                MenuItem { text: "芒果汁   ¥3" }
                MenuItem { text: "葡萄汁   ¥3" }
            }

            Menu
            {
                title: "饮料"
                MenuItem { text: "冰红茶   ¥3" }
                MenuItem { text: "矿泉水   ¥1" }
                MenuItem { text: "酸奶    ¥5" }
            }
        }
    }

三、Header 代码示例

header: TabBar
    {
        id: headerTabBar

        TabButton
        {
            text: "页面1"
        }
        TabButton
        {
            text: "页面2"
        }
        TabButton
        {
            text: "页面3"
        }
        TabButton
        {
            text: "页面4"
        }
        TabButton
        {
            text: "页面5"
        }
    }

四、注意 

Footer和Header同理,这里就不写代码演示了。在导入库的时候,如果同时需要用到Material和Dialog库,那么Material库要在前面导入,否则就会报错。

import QtQuick
import QtQuick.Controls.Material    //Material要放在Dialogs的前面,否则会报错
import QtQuick.Controls
import QtQuick.Layouts

还有一点就是,MenuBar、Header、Footer是针对ApplicationWindow而言的,在Window中是使用不了的。 

五、完整代码

import QtQuick
import QtQuick.Controls.Material    //Material要放在Dialogs的前面,否则会报错
import QtQuick.Controls
import QtQuick.Layouts

ApplicationWindow {
    id: root
    width: 800
    height: 600
    visible: true
    title: qsTr("欢迎光临角角の超市!")

    menuBar: MenuBar
    {
        Material.background: "#a8c0fd"

        Menu
        {
            title: "主食"
            MenuItem { text: "米饭" }
            MenuItem { text: "面条" }
            MenuItem { text: "米线" }
        }

        Menu
        {
            title: "蔬菜"
            MenuItem { text: "香葱" }
            MenuItem { text: "白菜" }
            MenuItem { text: "冬瓜" }
            MenuItem { text: "南瓜" }
        }

        Menu
        {
            title: "水果"
            MenuItem { text: "苹果" }
            MenuItem { text: "香蕉" }
            MenuItem { text: "哈密瓜" }
            MenuItem { text: "葡萄" }
            MenuItem { text: "草莓" }

            MenuSeparator{} //分割线

            MenuItem { text: "西瓜" }
            MenuItem { text: "樱桃" }
            MenuItem { text: "猕猴桃" }

            MenuSeparator{}

            MenuItem { text: "山竹" }
            MenuItem { text: "莲雾" }
            MenuItem { text: "小番茄" }
        }

        Menu
        {
            title: "饮品"

            Menu
            {
                title: "酒类"
                MenuItem { text: "白酒    ¥15" }
                MenuItem { text: "啤酒    ¥3" }
                MenuItem { text: "鸡尾酒   ¥9" }
            }

            Menu
            {
                title: "果汁"
                MenuItem { text: "西瓜汁   ¥2" }
                MenuItem { text: "芒果汁   ¥3" }
                MenuItem { text: "葡萄汁   ¥3" }
            }

            Menu
            {
                title: "饮料"
                MenuItem { text: "冰红茶   ¥3" }
                MenuItem { text: "矿泉水   ¥1" }
                MenuItem { text: "酸奶    ¥5" }
            }
        }
    }

    header: TabBar
    {
        id: headerTabBar

        TabButton
        {
            text: "页面1"
        }
        TabButton
        {
            text: "页面2"
        }
        TabButton
        {
            text: "页面3"
        }
        TabButton
        {
            text: "页面4"
        }
        TabButton
        {
            text: "页面5"
        }
    }

    StackLayout
    {
        currentIndex: headerTabBar.currentIndex //实现页面切换,注意:不能是StackView

        Rectangle
        {
            width: contentItem.width
            height: contentItem.height
            Text
            {
                text: "页面1:" + parent.width + "x" + parent.height
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
        Rectangle
        {
            width: contentItem.width
            height: contentItem.height
            Text
            {
                text: "页面2:" + parent.width + "x" + parent.height
                font.pixelSize: 30
                anchors.centerIn: parent
            }
            Pane
            {
                x: 20; y: 20
                width: 200; height: 300
                Material.elevation: 5
                Material.roundedScale: Material.MediumScale
            }
        }
        Rectangle
        {
            width: contentItem.width
            height: contentItem.height
            Text
            {
                text: "页面3:" + parent.width + "x" + parent.height
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
        Rectangle
        {
            width: contentItem.width
            height: contentItem.height
            Text
            {
                text: "页面4:" + parent.width + "x" + parent.height
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
        Rectangle
        {
            width: contentItem.width
            height: contentItem.height
            Text
            {
                text: "页面5:" + parent.width + "x" + parent.height
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
    }
}

运行效果:

若本文对你有所启发或帮助,不妨点个赞或者收藏一下,感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵呜角角

如果对你有所帮助,哪怕1毛钱~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值