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: "点赞"
            }
        }
    }
}

演示

视频讲解

相关推荐
Aphelios3801 小时前
Java全栈面试宝典:线程机制与Spring IOC容器深度解析
java·开发语言·jvm·学习·rbac
日暮南城故里2 小时前
Java学习------源码解析之StringBuilder
java·开发语言·学习·源码
@hdd3 小时前
Qt实现HTTP GET/POST/PUT/DELETE请求
qt·http
安全方案4 小时前
精心整理-2024最新网络安全-信息安全全套资料(学习路线、教程笔记、工具软件、面试文档).zip
笔记·学习·web安全
士别三日&&当刮目相看4 小时前
JAVA学习*Object类
java·开发语言·学习
阳光_你好5 小时前
详细说明Qt 中共享内存方法: QSharedMemory 对象
开发语言·数据库·qt
序属秋秋秋5 小时前
算法基础_基础算法【高精度 + 前缀和 + 差分 + 双指针】
c语言·c++·学习·算法
爱吃馒头爱吃鱼6 小时前
QML编程中的性能优化二
开发语言·qt·学习·性能优化
白夜易寒6 小时前
Docker学习之容器虚拟化与虚拟机的区别(day11)
学习·docker·容器
教练 我想学编程7 小时前
学习记录706@微信小程序+springboot项目 真机测试 WebSocket错误: {errMsg: Invalid HTTP status.}连接不上
spring boot·学习·微信小程序