Qt/QML学习-MenuBar&ToolBar

QML学习

main.qml

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("MenuBar&ToolBar")
    // 菜单栏
    MenuBar {
        id: menuBar
        contentWidth: parent.width
        // 绘制菜单栏背景
        background: Rectangle {
            color: "lightGray"
            Rectangle {
                width: parent.width
                height: 1
                anchors.bottom: parent.bottom
                color: "black"
            }
        }
        // 菜单选项代理
        delegate: MenuBarItem {
            // 绘制菜单选项背景
            background: Rectangle {
                color: highlighted? "black": "gray"
                border.width: 1
            }
            // 绘制菜单选项内容
            contentItem: Text {
                font.pointSize: 15
                color: "white"
                text: menu.title
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
            }
        }

        Menu {
            id: menu
            title: "菜单1"
            // 绘制每个选项视图
            delegate: MenuItem {
                id: menuItem
                // 绘制选项展开箭头
                arrow: Canvas  {
                    x: parent.width - width
                    implicitWidth: 40
                    implicitHeight: 40
                    visible: menuItem.subMenu
                    onPaint: {
                        var ctx = getContext("2d")
                        ctx.moveTo(12, 12)
                        ctx.lineTo(width - 12, height / 2)
                        ctx.lineTo(12, height - 12)
                        ctx.closePath()
                        ctx.fill()
                    }
                }
                // 绘制选项指示器
                indicator: Rectangle {
                    width: 24       // 14 + 10
                    height: 20
                    anchors.verticalCenter: parent.verticalCenter
                    color: "transparent"
                    Rectangle {
                        width: 14
                        height: 14
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: parent.left
                        anchors.leftMargin: 10
                        visible: menuItem.checked
                        color: "black"
                        radius: 7
                    }
                }
                // 绘制选项内容视图
                contentItem: Text {
                    leftPadding: menuItem.indicator.width
                    rightPadding: menuItem.arrow.width
                    text: menuItem.text
                    font.pointSize: 15
                    opacity: enabled ? 1.0 : 0.3
                    color: menuItem.highlighted ? "white" : "black"
                    horizontalAlignment: Text.AlignLeft
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }
                // 绘制选项背景视图
                background: Rectangle {
                    implicitWidth: 200
                    implicitHeight: 40
                    opacity: enabled ? 1 : 0.3
                    color: menuItem.highlighted ? "gray" : "transparent"
                }
            }
            Action {
                text: "选项1"
                checkable: true
                checked: true
            }
            MenuSeparator {}
            Action {
                text: "选项2"
            }
            Menu{
                title: "选项3"
                Action{ text: "选项4" }
                Action{ text: "选项5" }
            }
        }

        Menu {
            title: "About"
            Action {
                text: "选项1"
                checkable: true
            }
        }
    }
    // 工具栏
    ToolBar {
        anchors.top: menuBar.bottom
        width: parent.width

        Row {
            ToolButton {
               text: "关注"
               contentItem: Rectangle {
                   color: "transparent"
                   border.width: 1
                   Text {
                       anchors.centerIn: parent
                       text: parent.parent.text
                   }
               }
               padding: 0
            }
            ToolButton {
               text: "点赞"
            }
        }
    }
}

演示

视频讲解

相关推荐
Tbisnic3 分钟前
AI大模型学习第十一天:技术选型、安全防护与金融实战
python·学习·ai·大模型·提示词工程
xmtxz1 小时前
计算机网络基础课程学习心得:从理论抽象到硬核实战的进阶之路
运维·学习
YM52e3 小时前
男孩子在外自我保护指南——用鸿蒙 ArkTS 构建交互式安全教育应用
学习·安全·华为·harmonyos·鸿蒙·鸿蒙系统
aXin_ya3 小时前
Ai Vibecoding学习(各个AI的讲解)
学习
数据法师3 小时前
QuickSay :基于 Qt 的轻量级快捷短语管理工具
开发语言·qt
fanged4 小时前
Linux内核学习16--I2C子系统(TODO)
学习
.千余4 小时前
【C++】C++继承入门(下):友元、静态成员与菱形继承的底层逻辑
开发语言·c++·笔记·学习·其他
小短腿的代码世界4 小时前
行情快照与增量更新引擎:Qt在高频交易数据分发中的核心架构——你的行情推送为什么延迟了500ms?
开发语言·qt·架构
YJlio4 小时前
《Sysinternals实战指南》16.5 Ctrl2Cap 工具详解:把 Caps Lock 变成 Ctrl 的键盘改造与回退方法
linux·运维·服务器·网络·python·学习·计算机外设
DogDaoDao5 小时前
深入理解 Qt:从原理到实战的全景指南
开发语言·qt·程序员