随笔 - 67,  文章 - 1,  评论 - 2,  阅读 - 56291
前言
  `在实际的开发中,QML自带的组件基本上不会直接用,因为需要花里胡哨的样式,所以需要我们。`
运行效果

知识点
  • ApplicationWindow
  • Item
  • MouseArea
  • Button
实现思想
// <1> 隐藏原生窗口的系统标题栏
// Qt.Window >> 指定该部件为窗口属性。请注意,如果小部件没有父级,则无法取消设置此标志。
// Qt.FramelessWindowHint >> 设置窗口属性为无边框样式
flags: Qt.Window | Qt.FramelessWindowHint

<2> 自定义窗口标题栏样式
// 直接定义一个 Item 或者其他组件作为窗口的标题栏即可
// 窗口的id为 rootWindow
// 处理窗口 onTitleChanged 信号,同步到自定义标题栏即可
    Rectangle {

        id : rootWindowMenuBar
        width: rootWindow.width
        height: 40

        Text {
            id: rootWindowTitleBarTitleText
            text: rootWindow.title
        }
    }

<3> 自定义窗口标题栏事件
// MouseArea >> 不可见的组件,其功能提供鼠标处理信号
 MouseArea {

     MouseArea {

    anchors.fill: rootWindowTitleBar
    // 只处理鼠标左键
    acceptedButtons: Qt.LeftButton
    // 接收鼠标按下事件
    onPressed: {

        rootWindow.rootWindowTitleMousePos = Qt.point(mouseX,mouseY)
        rootWindow.isMoveWindow = true
    }

    // 鼠标释放,关闭窗口移动flag
    onReleased: {

        if(mouse.button === Qt.LeftButton){

            rootWindow.isMoveWindow = false
        }
    }

    //
    onMouseXChanged: {

        if(rootWindow.isMoveWindow){

            rootWindow.x += mouseX - rootWindow.rootWindowTitleMousePos.x;
        }
    }
    onMouseYChanged: {

        rootWindow.y += mouseY - rootWindow.rootWindowTitleMousePos.y;
    }
}

<4> 自定义窗口标题栏样式

Button {

	id : pushbtnWindowsClose
	width: 30
	height: 30
	anchors.margins: 5
	anchors.right: rootWindowTitleBar.right
	anchors.topMargin: 5
	anchors.verticalCenter: parent.verticalCenter
	text: "X"
}

Button {

	id : pushbtnWindowsMaximize
	width: 30
	height: 30
	anchors.margins: 5
	anchors.verticalCenter: parent.verticalCenter
	anchors.right: pushbtnWindowsClose.left
	text: "口"
}

Button {

	id : pushbtnWindowsMinimize
	width: 30
	height: 30
	anchors.margins: 5
	anchors.verticalCenter: parent.verticalCenter
	anchors.right: pushbtnWindowsMaximize.left
	text: "一"
}
完整QML代码
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5

ApplicationWindow {

    property bool isMoveWindow: false
    property point rootWindowTitleMousePos: Qt.point(x,y)

    id : rootWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    flags: Qt.Window | Qt.FramelessWindowHint

    onTitleChanged: rootWindowTitleBarTitleText.text = title

    Rectangle {

        id : rootWindowTitleBar
        width: rootWindow.width
        height: 40

        border.color: "red"
        color: "black"

        Text {
            id: rootWindowTitleBarTitleText
            text: rootWindow.title
            color: "white"
            anchors.verticalCenter: parent.verticalCenter
        }

        // 为窗口标题栏添加鼠标事件
        MouseArea {

            anchors.fill: rootWindowTitleBar
            // 只处理鼠标左键
            acceptedButtons: Qt.LeftButton
            // 接收鼠标按下事件
            onPressed: {

                rootWindow.rootWindowTitleMousePos = Qt.point(mouseX,mouseY)
                rootWindow.isMoveWindow = true
            }

            // 鼠标释放,关闭窗口移动flag
            onReleased: {

                if(mouse.button === Qt.LeftButton){

                    rootWindow.isMoveWindow = false
                }
            }

            //
            onMouseXChanged: {

                if(rootWindow.isMoveWindow){

                    rootWindow.x += mouseX - rootWindow.rootWindowTitleMousePos.x;
                }
            }
            onMouseYChanged: {

                rootWindow.y += mouseY - rootWindow.rootWindowTitleMousePos.y;
            }
        }

        Button {

            id : pushbtnWindowsClose
            width: 30
            height: 30
            anchors.margins: 5
            anchors.right: rootWindowTitleBar.right
            anchors.topMargin: 5
            anchors.verticalCenter: parent.verticalCenter
            text: "X"
        }

        Button {

            id : pushbtnWindowsMaximize
            width: 30
            height: 30
            anchors.margins: 5
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: pushbtnWindowsClose.left
            text: "口"
        }

        Button {

            id : pushbtnWindowsMinimize
            width: 30
            height: 30
            anchors.margins: 5
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: pushbtnWindowsMaximize.left
            text: "一"
        }
    }

}
posted on   怪小子  阅读(3064)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
              
点击右上角即可分享
微信分享提示